2

what is the usage of context in xv6 OS ? what is the job of each register in context ?

struct context {
  uint edi;
  uint esi;
  uint ebx;
  uint ebp;
  uint eip;
};
  • Either someone here would need to know the innards of xv6 or we'd need more information. One would presume that context is a structure for saving and restoring register values. If so, the job of each register in context is just to stay there unchanged. – user3344003 Jan 16 '16 at 19:46
  • How about grepping the code for `struct context` and seeing precisely how it's used? – Alexey Frunze Feb 28 '16 at 02:18
  • This [link](https://www.cse.iitb.ac.in/~mythili/os/anno_slides/lecture25.pdf) might be helpful. – Li-Guangda Jul 16 '23 at 09:30

2 Answers2

8

context is that set of information that allows you to resume execution of a task from the exact same point where it stopped due to context switch (i.e. the scheduler selects another task to run and suspends the current one, hence it has to save the execution information of the current task and restore those of the next task to run).

The purpose of each of those registers is:

  • edi: Destination index, for string operations
  • esi: Source index, for string operations
  • ebx: Base index, for use with arrays
  • ebp: Stack Base Pointer, for holding the address of the current stack frame
  • eip: Instruction Pointer, points to instruction to be executed
mike
  • 220
  • 1
  • 12
  • How come there's no EAX, ECX, EDX, EFLAGS, FPU registers in the structure? Don't they need to be saved and restored on a context switch? – Alexey Frunze Feb 28 '16 at 02:21
  • 1
    eax, ecx, edx are not saved because in x86 convention they are to be saved by the caller. I guess for FPU registers the same applies. For what concerns EFLAGS they are stored in the trapframe structure associated to the process (this works because you don't have threads in xv6, in case you have then yes I think you should put the eflags in context). – mike Feb 28 '16 at 11:17
  • 1
    When a hardware interrupt occurs (including those that trigger a switch between processes and threads), there's no caller. Code gets preempted at arbitrary points by interrupts and you can't just throw away EAX or EFLAGS because the interrupted code may have been in the middle of e.g. ADD EBX, EDX & ADC EAX, ECX. So, these registers would need to be saved and restored somewhere. In the trap frame most likely. So, why not use the trap frame for all these regs (we could defer the FPU/SSE context switch and deal with it separately)? – Alexey Frunze Feb 28 '16 at 11:27
  • 1
    Actually if you look at the code in [trapasm.S](https://github.com/guilleiguaran/xv6/blob/ff2783442ea2801a4bf6c76f198f36a6e985e7dd/trapasm.S), before calling the `trap()` function all registers are saved into the trapframe. – mike Feb 29 '16 at 21:20
  • 1
    Right. So, `swtch()` appears to be doing extra saving/restoring work. – Alexey Frunze Mar 01 '16 at 07:04
  • 2
    Nope. For example. If you are in user mode, the registers saved in trapframe are the user mode ones. The registers saved by swtch are always the kernel mode ones. – Morass Mar 02 '16 at 17:24
2

This structure is the kernel context of the running process.

The user mode context is saved into the trapframe structure.

Morass
  • 323
  • 1
  • 5