Sunday, July 7, 2013

CPU state transfer in QEMU live migration

The CPU migration state in QEMU is in target-i386/machine.c. This file defines all the CPU states needed to transfer to the target VM in struct const VMStateDescription vmstate_x86_cpu. This struct consists of many version control messages. Messages can be set in VMStateSubsection to ignore version problems.

Read the relevant codes for more details.

Friday, July 5, 2013

Format of GDT entry

The Global Descriptor Table (GDT) usually used to hold segment informations. But GDT can hold things other than segment descriptors, every 8-byte entry in GDT is a descriptor, which can be TSS descriptor, LDT descriptor, Call Gate descriptor. I will introduce segment descriptors in this blog, because the last three descriptors are rarely used in most operating systems. Linux uses a shared TSS and LDT descriptor for every task and imply its own call gates mechanism.

Firstly, GDT can be accessed by a GDT descriptor. This descriptor is not the same as descriptors cited before, it is an assembly structure with a 2-byte size and a 4-byte offset. 2-byte size saves total entries in GDT and 4-byte offset refers to the first entry of GDT. So this means GDT entry can only be set on the first 4G memory.

Each entry of segment descriptor in GDT has a complex structure:

Here are three Base fields refers to the base address of a segment. What "Limit 0:15" means is that the field contains bits 0-15 of the limit value. The base is a 32 bit value containing the linear address where the segment begins. The limit, a 20 bit value, tells the maximum addressable unit (either in 1 byte units, or in pages). Hence, if you choose page granularity (4 KiB) and set the limit value to 0xFFFFF the segment will span the full 4 GiB address space. Here is the structure of the access byte and flags:


The bit fields are:
  • Pr: Present bit. This must be 1 for all valid selectors.
  • Privl: Privilege, 2 bits. Contains the ring level, 0 = highest (kernel), 3 = lowest (user applications).
  • Ex: Executable bit. If 1 code in this segment can be executed, ie. a code selector. If 0 it is a data selector.
  • DC: Direction bit/Conforming bit.
    • Direction bit for data selectors: Tells the direction. 0 the segment grows up. 1 the segment grows down, ie. the offset has to be greater than the base.
    • Conforming bit for code selectors:
      • If 1 code in this segment can be executed from an equal or lower privilege level. For example, code in ring 3 can far-jump to conforming code in a ring 2 segment. Theprivl-bits represent the highest privilege level that is allowed to execute the segment. For example, code in ring 0 cannot far-jump to a conforming code segment withprivl==0x2, while code in ring 2 and 3 can. Note that the privilege level remains the same, ie. a far-jump form ring 3 to a privl==2-segment remains in ring 3 after the jump.
      • If 0 code in this segment can only be executed from the ring set in privl.
  • RW: Readable bit/Writable bit.
    • Readable bit for code selectors: Whether read access for this segment is allowed. Write access is never allowed for code segments.
    • Writable bit for data selectors: Whether write access for this segment is allowed. Read access is always allowed for data segments.
  • Ac: Accessed bit. Just set to 0. The CPU sets this to 1 when the segment is accessed.
  • Gr: Granularity bit. If 0 the limit is in 1 B blocks (byte granularity), if 1 the limit is in 4 KiB blocks (page granularity).
  • Sz: Size bit. If 0 the selector defines 16 bit protected mode. If 1 it defines 32 bit protected mode. You can have both 16 bit and 32 bit selectors at once.
Not shown in the picture is the 'L' bit (bit 21, next to 'Sz') which is used for x86-64 mode. See table 3-1 of the Intel Architecture manual.

Refer to Intel Manual 3.4.5 for more details.

Some materials are cited from http://wiki.osdev.org/Global_Descriptor_Table

Tuesday, July 2, 2013

Intel Control Registers

Control registers of Intel CPU, including CR0/CR1/CR2/CR3/CR4/CR8, determine operating mode of the processor and the characteristics of the currently executing task. Most of the contents of this blog is cited from Intel manual Volume 3C.

All the CRx registers can be written by MOV, but only support moving from general registers to CRx, any move from memory may cause error when assembling. In 64-bit mode, control registers are expanded to 64 bits width. But the upper bits are always not used and set zero.

  • CR0 - Contains system control flags that control operating mode and states of the processor.
  • CR1 - Reserved.
  • CR2 - Contains the page-fault linear address (the linear address that caused a page fault).
  • CR3 - Contains the physical address of the base of the paging-structure hierarchy and two flags (PCD and PWT). The lower 12 bits of address are supposed to be 0 because they are for page offset while CR3 contains page directory. Then these bits store flags of page table.
  • CR4 - Contains a group of flags that enable several architectural extensions, and indicate operating system or executive support for specific processor capabilities.
  • CR8 - This register is available only in 64-bit mode. Provides read and write access to the Task Priority Register (TPR). It specifies the priority threshold value that operating systems use to control the priority class of external interrupts allowed to interrupt the processor.
One of the most import bit(s) in CR0 is PG and PE bit. PE(Protection Enable) bit is the bit 0 of CR0, which enables protected mode when set. It enables segment-level protection. PG(Paging) bit is the bit 31 of CR0, which enables paging when set. PG bit can be set only when PE is set. Set to these two bits means entering protected mode.

For more information about bits in control registers, you can refer to Intel Manual Volume 3C, chapter 2 System Architecture Overview.


Friday, June 28, 2013

What is GDT/LDT/IDT/TSS

GDT, LDT, IDT and TSS are all data structures specified by Intel x86 architecture in memory management module.

GDT, Global Descriptor Table, is used to define the characteristics of the various memory areas used during program execution, including the base address, the size and access privileges like executability and writability. These memory areas are called segments in Intel terminology. (From Wikipedia) Segment is a term for memory management in Intel x86 architecture, which is also used collaboratively with paging mechanism.

LDT, Local Descriptor Table, acts similar to GDT, which also saves segments descriptor. The main differences between GDT and LDT is: 1) GDT have only one copy in system while LDT can have many, 2) GDT may not changed during execution which LDT often changes when task switches, 3) entry of LDT is save in GDT. Entries in GDT and LDT have the same structure.

IDT, Interrupt Descriptor Table, is a data structure used by the x86 architecture to implement an interrupt vector table. The IDT is used by the processor to determine the correct response to interrupts and exceptions. Use of the IDT is triggered by three types of events: hardware interrupts, software interrupts, and processor exceptions, which together are referred to as "interrupts". The IDT consists of 256 interrupt vectors–the first 32 (0-31 or 00-1F) of which are reserved for processor exceptions. (From Wikipedia)

TSS, Task State Segment, is a special structure on x86-based computers which holds information about a task. It is used by the operating system kernel for task management. The TSS may reside anywhere in memory. A special segment register called the task register (TR) holds a segment selector that points to a valid TSS segment descriptor which resides in the GDT (a TSS descriptor may not reside in the LDT). The TSS may contain saved values of all the x86 registers. This is used for task switching. The operating system may load the TSS with the values of the registers that the new task needs and after executing a hardware task switch (such as with an IRET instruction) the x86 CPU will load the saved values from the TSS into the appropriate registers.

Note that only IDT is necessary for a CPU running normally. But actually, GDT is also used to store globally shared memory message. LDT is actually not used in Linux (every task used a shared default LDT). TSS is also used the same as LDT -- all tasks use a shared TSS. Before Linux 2.4, TSS is used for task switching. But every entry of TSS and LDT should be saved in GDT, which limits the total task numbers in the system. Linux uses software mechanism to manage task switching.

Thursday, June 27, 2013

How to build a micro hypervisor

I'm trying to find out how to build a micro hypervisor these days. As to what I know, building a micro hypervisor needs at least 5 parts as follows:

  1. CPU management. This part consists physical CPU (pCPU) management and virtual CPU (vCPU) management. Every pCPU should be initialed first and each CPU of VM needs a structure to management its flags, registers and states.
  2. Memory management. Memory management and isolation protection is another key feature of hypervisor. Hypervisor needs to know which part of physical memory is allocated to separated VM. Some advanced feature such as buddy system and memory ballooning may also be applied in hypervisor. 
  3. Interrupt control and exception handling. This part controls interrupts and exceptions caused in VM. This part mainly handles VMEXIT and inserts interrupts when VMENTER.
  4. Scheduling. This is a feature to support scheduling vCPUs on pCPUs.
  5. Communication. Communication between hypervisor and VM, as well as between VMs, is also another feature supported by hypervisor. Messages transferred between different domains should be handled by each sides. A message queue is used in Xen to support asynchronous communication.
In our test cases, we just need a much more simple hypervisor. We just need CPU management, memory management and interrupt control at first. Switching between different VMs can be supported in later version.

Tuesday, June 25, 2013

Footprint - June 25th

Actually, after all the final exams it is the first day for me to coding today. As what I have discussed with Jan last week, it seems to be a hard startup for our project.

First, I need to build up a tiny hypervisor which can support simple functions for nested virtualization test cases. Here is a simple tiny hypervisor named NOVA (https://github.com/IntelLabs/NOVA). I'm now reading through all NOVA's code. But unfortunately NOVA is written in C++! I have almost forget how to write inheritance! So I need "C++ Primer" by hand when reading codes ;(. I planned to thoroughly understand NOVA's code and write an abstract of implementing tiny hypervisor before IRC meet of this weekend.

It is sad that I have not received my welcome package! Some of my friends received request of custom declaration but I haven't got any message or phone call. I will ask Carol for my trace number of FedEx and then call FedEx for details of my package.

I'm itch for new lens, Sigma 10-20mm/F4~5.6, using on my D7000. I'm now staggering, maybe because the payment of GSoC has not come to hand.^_^

Configuring git send-email to use Gmail SMTP

Firstly, apt-get install git-email.
Then, add the correct configuration variables with the following:
$ git config --global sendemail.smtpserver smtp.gmail.com
$ git config --global sendemail.smtpserverport 587
$ git config --global sendemail.smtpencryption tls
$ git config --global sendemail.smtpuser your_email@gmail.com
Now it’s ready to use git send-email. Password is needed each time you send email.
P.S. Before using git send-email, you should use git format-patch first.
git format-patch -M origin/master
git send-email --to xxx@xxx.com --cc xxx@xxx.com 0001-xxx.patch


git config --global user.email "yzt356@gmail.com"
git config --global user.name "Arthur Chunqi Li"