Skip to content

Commit

Permalink
Avoid "boot" in xv6
Browse files Browse the repository at this point in the history
  • Loading branch information
Frans Kaashoek authored and Frans Kaashoek committed Aug 16, 2011
1 parent 9449646 commit a4b213c
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -110,7 +110,7 @@ initcode: initcode.S
$(OBJDUMP) -S initcode.o > initcode.asm

kernel: $(OBJS) entry.o data.o entryother initcode
$(LD) $(LDFLAGS) -T kernel.ld -e multiboot_entry -o kernel entry.o data.o $(OBJS) -b binary initcode entryother
$(LD) $(LDFLAGS) -T kernel.ld -e entry -o kernel entry.o data.o $(OBJS) -b binary initcode entryother
$(OBJDUMP) -S kernel > kernel.asm
$(OBJDUMP) -t kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > kernel.sym

Expand Down
2 changes: 1 addition & 1 deletion defs.h
Expand Up @@ -62,7 +62,7 @@ extern uchar ioapicid;
void ioapicinit(void);

// kalloc.c
char* boot_alloc(void);
char* enter_alloc(void);
char* kalloc(void);
void kfree(char*);
void kinit(void);
Expand Down
10 changes: 5 additions & 5 deletions entry.S
Expand Up @@ -34,17 +34,17 @@ multiboot_header:
.long multiboot_header
.long edata
.long end
.long multiboot_entry
.long entry

# Multiboot entry point. Machine is mostly set up.
.globl multiboot_entry
multiboot_entry:
# Entering xv6 on boot processor. Machine is mostly set up.
.globl entry
entry:
# Turn on page size extension for 4Mbyte pages
movl %cr4, %eax
orl $(CR4_PSE), %eax
movl %eax, %cr4
# Set page directory
movl $(V2P_WO(bootpgdir)), %eax
movl $(V2P_WO(enterpgdir)), %eax
movl %eax, %cr3
# Turn on paging.
movl %cr0, %eax
Expand Down
10 changes: 5 additions & 5 deletions entryother.S
Expand Up @@ -14,8 +14,8 @@
# Bootothers (in main.c) sends the STARTUPs one at a time.
# It copies this code (start) at 0x7000.
# It puts the address of a newly allocated per-core stack in start-4,
# the address of the place to jump to (mpboot) in start-8, and the physical
# address of bootpgdir in start-12.
# the address of the place to jump to (mpenter) in start-8, and the physical
# address of enterpgdir in start-12.
#
#
# This code is identical to bootasm.S except:
Expand Down Expand Up @@ -54,17 +54,17 @@ start32:
movl %cr4, %eax
orl $(CR4_PSE), %eax
movl %eax, %cr4
# Use bootpgdir as our initial page table
# Use enterpgdir as our initial page table
movl (start-12), %eax
movl %eax, %cr3
# Turn on paging.
movl %cr0, %eax
orl $(CR0_PE|CR0_PG|CR0_WP), %eax
movl %eax, %cr0

# Switch to the stack allocated by enterothers()
# Switch to the stack allocated by startothers()
movl (start-4), %esp
# Call mpboot()
# Call mpenter()
call *(start-8)

movw $0x8a00, %ax
Expand Down
6 changes: 3 additions & 3 deletions kalloc.c
Expand Up @@ -21,15 +21,15 @@ struct {
extern char end[]; // first address after kernel loaded from ELF file
char *newend;

// simple page allocator to get off the ground during boot
// simple page allocator to get off the ground during entry
char *
boot_alloc(void)
enter_alloc(void)
{
if (newend == 0)
newend = end;

if ((uint) newend >= KERNBASE + 0x400000)
panic("only first 4Mbyte are mapped during boot");
panic("only first 4Mbyte are mapped during entry");
void *p = (void*)PGROUNDUP((uint)newend);
memset(p, 0, PGSIZE);
newend = newend + PGSIZE;
Expand Down
4 changes: 2 additions & 2 deletions lapic.c
Expand Up @@ -132,7 +132,7 @@ microdelay(int us)

#define IO_RTC 0x70

// Start additional processor running bootstrap code at addr.
// Start additional processor running entry code at addr.
// See Appendix B of MultiProcessor Specification.
void

Expand All @@ -158,7 +158,7 @@ lapicstartap(uchar apicid, uint addr)
lapicw(ICRLO, INIT | LEVEL);
microdelay(100); // should be 10ms, but too slow in Bochs!

// Send startup IPI (twice!) to enter bootstrap code.
// Send startup IPI (twice!) to enter code.
// Regular hardware is supposed to only accept a STARTUP
// when it is in the halted state due to an INIT. So the second
// should be ignored, but it is part of the official Intel algorithm.
Expand Down
50 changes: 21 additions & 29 deletions main.c
Expand Up @@ -6,7 +6,7 @@
#include "proc.h"
#include "x86.h"

static void enterothers(void);
static void startothers(void);
static void mpmain(void) __attribute__((noreturn));
extern pde_t *kpgdir;

Expand All @@ -33,7 +33,7 @@ main(void)
ideinit(); // disk
if(!ismp)
timerinit(); // uniprocessor timer
enterothers(); // start other processors (must come before kinit; must use boot_alloc)
startothers(); // start other processors (must come before kinit; must use enter_alloc)
kinit(); // initialize memory allocator
userinit(); // first user process (must come after kinit)
// Finish setting up this processor in mpmain.
Expand All @@ -42,7 +42,7 @@ main(void)

// Other CPUs jump here from entryother.S.
static void
mpboot(void)
mpenter(void)
{
switchkvm();
seginit();
Expand All @@ -56,22 +56,22 @@ mpmain(void)
{
cprintf("cpu%d: starting\n", cpu->id);
idtinit(); // load idt register
xchg(&cpu->booted, 1); // tell enterothers() we're up
xchg(&cpu->started, 1); // tell startothers() we're up
scheduler(); // start running processes
}

pde_t bootpgdir[];
pde_t enterpgdir[];

// Start the non-boot processors.
// Start the non-boot (AP) processors.
static void
enterothers(void)
startothers(void)
{
extern uchar _binary_entryother_start[], _binary_entryother_size[];
uchar *code;
struct cpu *c;
char *stack;

// Write bootstrap code to unused memory at 0x7000.
// Write entry code to unused memory at 0x7000.
// The linker has placed the image of entryother.S in
// _binary_entryother_start.
code = p2v(0x7000);
Expand All @@ -81,44 +81,36 @@ enterothers(void)
if(c == cpus+cpunum()) // We've started already.
continue;

// Tell entryother.S what stack to use, the address of mpboot and pgdir;
// Tell entryother.S what stack to use, the address of mpenter and pgdir;
// We cannot use kpgdir yet, because the AP processor is running in low
// memory, so we use bootpgdir for the APs too. kalloc can return addresses
// memory, so we use enterpgdir for the APs too. kalloc can return addresses
// above 4Mbyte (the machine may have much more physical memory than 4Mbyte), which
// aren't mapped by bootpgdir, so we must allocate a stack using boot_alloc();
// aren't mapped by enterpgdir, so we must allocate a stack using enter_alloc();
// This introduces the constraint that xv6 cannot use kalloc until after these
// last boot_alloc invocations.
stack = boot_alloc();
// last enter_alloc invocations.
stack = enter_alloc();
*(void**)(code-4) = stack + KSTACKSIZE;
*(void**)(code-8) = mpboot;
*(int**)(code-12) = (void *) v2p(bootpgdir);
*(void**)(code-8) = mpenter;
*(int**)(code-12) = (void *) v2p(enterpgdir);

lapicstartap(c->id, v2p(code));

// wait for cpu to finish mpmain()
while(c->booted == 0)
while(c->started == 0)
;
}
}

// Boot page table used in multiboot.S and entryother.S.
// Boot page table used in entry.S and entryother.S.
// Page directories (and page tables), must start on a page boundary,
// hence the "__aligned__" attribute. Also, because of restrictions
// related to linking and static initializers, we use "x + PTE_P"
// here, rather than the more standard "x | PTE_P". Everywhere else
// you should use "|" to combine flags.
// hence the "__aligned__" attribute.
// Use PTE_PS in page directory entry to enable 4Mbyte pages.
pte_t dev_pgtable[NPTENTRIES];
pte_t entry_pgtable[NPTENTRIES];

__attribute__((__aligned__(PGSIZE)))
pde_t bootpgdir[NPDENTRIES] = {
pde_t enterpgdir[NPDENTRIES] = {
// Map VA's [0, 4MB) to PA's [0, 4MB)
[0]
= (0) + PTE_P + PTE_W + PTE_PS,
[0] = (0) + PTE_P + PTE_W + PTE_PS,
// Map VA's [KERNBASE, KERNBASE+4MB) to PA's [0, 4MB)
[KERNBASE>>PDXSHIFT]
= (0) + PTE_P + PTE_W + PTE_PS,
[KERNBASE>>PDXSHIFT] = (0) + PTE_P + PTE_W + PTE_PS,
};

//PAGEBREAK!
Expand Down
2 changes: 1 addition & 1 deletion mp.c
@@ -1,4 +1,4 @@
// Multiprocessor bootstrap.
// Multiprocessor support
// Search memory for MP description structures.
// http://developer.intel.com/design/pentium/datashts/24201606.pdf

Expand Down
2 changes: 1 addition & 1 deletion proc.h
Expand Up @@ -7,7 +7,7 @@ struct cpu {
struct context *scheduler; // swtch() here to enter scheduler
struct taskstate ts; // Used by x86 to find stack for interrupt
struct segdesc gdt[NSEGS]; // x86 global descriptor table
volatile uint booted; // Has the CPU started?
volatile uint started; // Has the CPU started?
int ncli; // Depth of pushcli nesting.
int intena; // Were interrupts enabled before pushcli?

Expand Down
2 changes: 1 addition & 1 deletion sysproc.c
Expand Up @@ -77,7 +77,7 @@ sys_sleep(void)
}

// return how many clock tick interrupts have occurred
// since boot.
// since start.
int
sys_uptime(void)
{
Expand Down
4 changes: 2 additions & 2 deletions vm.c
Expand Up @@ -12,7 +12,7 @@ pde_t *kpgdir; // for use in scheduler()
struct segdesc gdt[NSEGS];

// Set up CPU's kernel segment descriptors.
// Run once at boot time on each CPU.
// Run once on entry on each CPU.
void
seginit(void)
{
Expand Down Expand Up @@ -146,7 +146,7 @@ setupkvm(char* (*alloc)(void))
void
kvmalloc(void)
{
kpgdir = setupkvm(boot_alloc);
kpgdir = setupkvm(enter_alloc);
switchkvm();
}

Expand Down

0 comments on commit a4b213c

Please sign in to comment.