Skip to content

Commit

Permalink
Map kernel high
Browse files Browse the repository at this point in the history
Very important to give qemu memory through PHYSTOP :(
  • Loading branch information
Frans Kaashoek authored and Frans Kaashoek committed Jul 29, 2011
1 parent dccb915 commit 9aa0337
Show file tree
Hide file tree
Showing 20 changed files with 207 additions and 70 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ initcode: initcode.S
$(OBJDUMP) -S initcode.o > initcode.asm

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

Expand Down Expand Up @@ -200,7 +200,7 @@ QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \
ifndef CPUS
CPUS := 2
endif
QEMUOPTS = -hdb fs.img xv6.img -smp $(CPUS)
QEMUOPTS = -hdb fs.img xv6.img -smp $(CPUS) -m 512

qemu: fs.img xv6.img
$(QEMU) -serial mon:stdio $(QEMUOPTS)
Expand Down
11 changes: 6 additions & 5 deletions bootmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "types.h"
#include "elf.h"
#include "x86.h"
#include "memlayout.h"

#define SECTSIZE 512

Expand All @@ -19,7 +20,7 @@ bootmain(void)
struct elfhdr *elf;
struct proghdr *ph, *eph;
void (*entry)(void);
uchar* va;
uchar* pa;

elf = (struct elfhdr*)0x10000; // scratch space

Expand All @@ -34,15 +35,15 @@ bootmain(void)
ph = (struct proghdr*)((uchar*)elf + elf->phoff);
eph = ph + elf->phnum;
for(; ph < eph; ph++){
va = (uchar*)ph->va;
readseg(va, ph->filesz, ph->offset);
pa = (uchar*)ph->pa;
readseg(pa, ph->filesz, ph->offset);
if(ph->memsz > ph->filesz)
stosb(va + ph->filesz, 0, ph->memsz - ph->filesz);
stosb(pa + ph->filesz, 0, ph->memsz - ph->filesz);
}

// Call the entry point from the ELF header.
// Does not return!
entry = (void(*)(void))(elf->entry);
entry = (void(*)(void))(elf->entry & 0xFFFFFF);
entry();
}

Expand Down
14 changes: 9 additions & 5 deletions bootother.S
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "asm.h"
#include "memlayout.h"

# Each non-boot CPU ("AP") is started up in response to a STARTUP
# IPI from the boot CPU. Section B.4.2 of the Multi-Processor
Expand All @@ -24,6 +25,8 @@

#define CR0_PE 1

#define RELOC1(x) ((x) + KERNBASE) // same as V2P, but without casts

.code16
.globl start
start:
Expand All @@ -40,7 +43,7 @@ start:
movl %eax, %cr0

//PAGEBREAK!
ljmp $(SEG_KCODE<<3), $start32
ljmpl $(SEG_KCODE<<3), $(start32+KERNBASE)

.code32
start32:
Expand All @@ -53,10 +56,10 @@ start32:
movw %ax, %gs

# switch to the stack allocated by bootothers()
movl start-4, %esp
movl RELOC1(start-4), %esp

# call mpmain()
call *(start-8)
call *(RELOC1(start)-8)

movw $0x8a00, %ax
movw %ax, %dx
Expand All @@ -69,8 +72,9 @@ spin:
.p2align 2
gdt:
SEG_NULLASM
SEG_ASM(STA_X|STA_R, 0x0, 0xffffffff)
SEG_ASM(STA_W, 0x0, 0xffffffff)
SEG_ASM(STA_X|STA_R, -KERNBASE, 0xffffffff)
SEG_ASM(STA_W, -KERNBASE, 0xffffffff)


gdtdesc:
.word (gdtdesc - gdt - 1)
Expand Down
6 changes: 5 additions & 1 deletion console.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "spinlock.h"
#include "fs.h"
#include "file.h"
#include "memlayout.h"
#include "mmu.h"
#include "proc.h"
#include "x86.h"
Expand Down Expand Up @@ -60,6 +61,9 @@ cprintf(char *fmt, ...)
if(locking)
acquire(&cons.lock);

if (fmt == 0)
panic("null fmt");

argp = (uint*)(void*)(&fmt + 1);
state = 0;
for(i = 0; (c = fmt[i] & 0xff) != 0; i++){
Expand Down Expand Up @@ -121,7 +125,7 @@ panic(char *s)
//PAGEBREAK: 50
#define BACKSPACE 0x100
#define CRTPORT 0x3d4
static ushort *crt = (ushort*)0xb8000; // CGA memory
static ushort *crt = (ushort*)P2V(0xb8000); // CGA memory

static void
cgaputc(int c)
Expand Down
2 changes: 2 additions & 0 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ extern uchar ioapicid;
void ioapicinit(void);

// kalloc.c
char* pgalloc(void);
char* kalloc(void);
void kfree(char*);
void kinit(void);
Expand Down Expand Up @@ -160,6 +161,7 @@ void uartintr(void);
void uartputc(int);

// vm.c
void pginit(char* (*alloc)());
void seginit(void);
void kvmalloc(void);
void vmenable(void);
Expand Down
1 change: 1 addition & 0 deletions exec.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "types.h"
#include "param.h"
#include "memlayout.h"
#include "mmu.h"
#include "proc.h"
#include "defs.h"
Expand Down
1 change: 1 addition & 0 deletions ide.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "types.h"
#include "defs.h"
#include "param.h"
#include "memlayout.h"
#include "mmu.h"
#include "proc.h"
#include "x86.h"
Expand Down
22 changes: 19 additions & 3 deletions kalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "types.h"
#include "defs.h"
#include "param.h"
#include "memlayout.h"
#include "mmu.h"
#include "spinlock.h"

Expand All @@ -18,6 +19,20 @@ struct {
} kmem;

extern char end[]; // first address after kernel loaded from ELF file
char *newend;

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

void *p = (void*)PGROUNDUP((uint)newend);
memset(p, 0, PGSIZE);
newend = newend + PGSIZE;
return p;
}

// Initialize free list of physical pages.
void
Expand All @@ -26,8 +41,8 @@ kinit(void)
char *p;

initlock(&kmem.lock, "kmem");
p = (char*)PGROUNDUP((uint)end);
for(; p + PGSIZE <= (char*)PHYSTOP; p += PGSIZE)
p = (char*)PGROUNDUP((uint)newend);
for(; p + PGSIZE <= (char*)p2v(PHYSTOP); p += PGSIZE)
kfree(p);
}

Expand All @@ -41,7 +56,7 @@ kfree(char *v)
{
struct run *r;

if((uint)v % PGSIZE || v < end || (uint)v >= PHYSTOP)
if((uint)v % PGSIZE || v < end || v2p(v) >= PHYSTOP)
panic("kfree");

// Fill with junk to catch dangling refs.
Expand All @@ -67,6 +82,7 @@ kalloc(void)
if(r)
kmem.freelist = r->next;
release(&kmem.lock);
cprintf("kalloc: 0x%x\n", r);
return (char*)r;
}

33 changes: 23 additions & 10 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "types.h"
#include "defs.h"
#include "param.h"
#include "memlayout.h"
#include "mmu.h"
#include "proc.h"
#include "x86.h"
Expand All @@ -9,13 +10,16 @@ static void bootothers(void);
static void mpmain(void);
void jmpkstack(void) __attribute__((noreturn));
void mainc(void);
static volatile int newpgdir;


// Bootstrap processor starts running C code here.
// Allocate a real stack and switch to it, first
// doing some setup required for memory allocator to work.
int
main(void)
{
pginit(pgalloc);
mpinit(); // collect info about this machine
lapicinit(mpbcpu());
seginit(); // set up segments
Expand Down Expand Up @@ -46,7 +50,6 @@ mainc(void)
ioapicinit(); // another interrupt controller
consoleinit(); // I/O devices & their interrupts
uartinit(); // serial port
kvmalloc(); // initialize the kernel page table
pinit(); // process table
tvinit(); // trap vectors
binit(); // buffer cache
Expand All @@ -57,7 +60,8 @@ mainc(void)
timerinit(); // uniprocessor timer
userinit(); // first user process
bootothers(); // start other processors

kvmalloc(); // new kernel page table wo. bottom mapped
newpgdir = 1;
// Finish setting up this processor in mpmain.
mpmain();
}
Expand All @@ -66,16 +70,25 @@ mainc(void)
// Bootstrap CPU comes here from mainc().
// Other CPUs jump here from bootother.S.
static void
mpmain(void)
mpboot(void)
{
if(cpunum() != mpbcpu()){
seginit();
lapicinit(cpunum());
}
vmenable(); // turn on paging
seginit();
lapicinit(cpunum());
mpmain();
}

// Common CPU setup code.
// Bootstrap CPU comes here from mainc().
// Other CPUs jump here from bootother.S.
static void
mpmain(void)
{
cprintf("cpu%d: starting\n", cpu->id);
idtinit(); // load idt register
xchg(&cpu->booted, 1); // tell bootothers() we're up
while (!newpgdir) ; // wait until we have new page dir
switchkvm(); // switch to new page dir
scheduler(); // start running processes
}

Expand All @@ -91,7 +104,7 @@ bootothers(void)
// Write bootstrap code to unused memory at 0x7000.
// The linker has placed the image of bootother.S in
// _binary_bootother_start.
code = (uchar*)0x7000;
code = p2v(0x7000);
memmove(code, _binary_bootother_start, (uint)_binary_bootother_size);

for(c = cpus; c < cpus+ncpu; c++){
Expand All @@ -103,9 +116,9 @@ bootothers(void)
// its first instruction.
stack = kalloc();
*(void**)(code-4) = stack + KSTACKSIZE;
*(void**)(code-8) = mpmain;
*(void**)(code-8) = mpboot;

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

// Wait for cpu to finish mpmain()
while(c->booted == 0)
Expand Down
8 changes: 0 additions & 8 deletions mmu.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,10 @@ struct segdesc {
// construct linear address from indexes and offset
#define PGADDR(d, t, o) ((uint)((d) << PDXSHIFT | (t) << PTXSHIFT | (o)))

// turn a kernel linear address into a physical address.
// all of the kernel data structures have linear and
// physical addresses that are equal.
#define PADDR(a) ((uint)(a))

// Page directory and page table constants.
#define NPDENTRIES 1024 // page directory entries per page directory
#define NPTENTRIES 1024 // page table entries per page table

#define PGSIZE 4096 // bytes mapped by a page
#define PGSHIFT 12 // log2(PGSIZE)

#define PTXSHIFT 12 // offset of PTX in a linear address
#define PDXSHIFT 22 // offset of PDX in a linear address

Expand Down
4 changes: 3 additions & 1 deletion mp.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "types.h"
#include "defs.h"
#include "param.h"
#include "memlayout.h"
#include "mp.h"
#include "x86.h"
#include "mmu.h"
Expand Down Expand Up @@ -39,6 +40,7 @@ mpsearch1(uchar *addr, int len)
{
uchar *e, *p;

addr = p2v((uint) addr);
e = addr+len;
for(p = addr; p < e; p += sizeof(struct mp))
if(memcmp(p, "_MP_", 4) == 0 && sum(p, sizeof(struct mp)) == 0)
Expand Down Expand Up @@ -83,7 +85,7 @@ mpconfig(struct mp **pmp)

if((mp = mpsearch()) == 0 || mp->physaddr == 0)
return 0;
conf = (struct mpconf*)mp->physaddr;
conf = (struct mpconf*) p2v((uint) mp->physaddr);
if(memcmp(conf, "PCMP", 4) != 0)
return 0;
if(conf->version != 1 && conf->version != 4)
Expand Down
11 changes: 7 additions & 4 deletions multiboot.S
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
# }

#include "asm.h"
#include "memlayout.h"

#define RELOC(x) ((x) - KERNBASE) // same as V2P, but without casts

#define STACK 4096

Expand Down Expand Up @@ -42,7 +45,7 @@ multiboot_header:
# boot loader - bootasm.S - sets up.
.globl multiboot_entry
multiboot_entry:
lgdt gdtdesc
lgdt RELOC(gdtdesc)
ljmp $(SEG_KCODE<<3), $mbstart32

mbstart32:
Expand All @@ -65,11 +68,11 @@ spin:
.p2align 2 # force 4 byte alignment
gdt:
SEG_NULLASM # null seg
SEG_ASM(STA_X|STA_R, 0x0, 0xffffffff) # code seg
SEG_ASM(STA_W, 0x0, 0xffffffff) # data seg
SEG_ASM(STA_X|STA_R, -KERNBASE, 0xffffffff) # code seg
SEG_ASM(STA_W, -KERNBASE, 0xffffffff) # data seg

gdtdesc:
.word (gdtdesc - gdt - 1) # sizeof(gdt) - 1
.long gdt # address gdt
.long RELOC(gdt) # address gdt

.comm stack, STACK
Loading

0 comments on commit 9aa0337

Please sign in to comment.