2

I am trying to implement kernel level threads in xv6.

My main problem at the moment is to understand how the CPU gets its information about the current process and how to modify it to point to the current thread instead.

I know it is somehow linked to this line:

extern struct proc *proc asm("%gs:4");

in proc.h, but I do not fully understand how and why it works.

livibetter
  • 19,832
  • 3
  • 42
  • 42
Shperb
  • 474
  • 7
  • 19
  • 1
    I don't know what xv6 is, but that looks like x86 code. On the x86, gs is a segment register. It commonly points to a fixed location that contains information about the current [thread](http://stackoverflow.com/a/10810340/2189500). The 4 indicates an offset 4 bytes into that location. So referencing this variable will reference the data 4bytes in to the thread data. The format of the data at gs is OS-specific. On Windows, look at NT_TIB in winnt.h. Not sure where to find this info on linux. – David Wohlferd Apr 21 '15 at 21:23

1 Answers1

3

I found out %gs points to to the line struct cpu *cpu; in the struct cpu (defined at proc.h), and right below that line (+ 4 bytes after the cpu pointer) the current process of the cpu is stored: struct proc *proc; // The currently-running process. so in order to add thread support one should either alter this line to point to the new thread struct instead of process struct or alternatively, add the thread below the "proc" line and perform the following changes:

  1. add in proc.h the following decleration: extern struct thread *thread asm("%gs:8");
  2. change in vm.c, in fucntion "seginit(void)" the line c->gdt[SEG_KCPU] = SEG(STA_W, &c->cpu, 8, 0); to c->gdt[SEG_KCPU] = SEG(STA_W, &c->cpu, 12, 0); in order to allocate space for the extra thread pointer.
Shperb
  • 474
  • 7
  • 19