提交 f56e01e3 创建 作者: Frans Kaashoek's avatar Frans Kaashoek

Merge branch 'scale-amd64' of ssh://amsterdam.csail.mit.edu/home/am0/6.828/xv6 into scale-amd64

Conflicts: main.c
...@@ -86,7 +86,7 @@ start32: ...@@ -86,7 +86,7 @@ start32:
call initpagetables call initpagetables
call init32e call init32e
movl $PADDR(start64), %edi movl $PADDR(start64), %eax
# Enter 64-bit mode. # Enter 64-bit mode.
ljmp $KCSEG, $PADDR(tramp64) // code64 segment selector ljmp $KCSEG, $PADDR(tramp64) // code64 segment selector
...@@ -107,7 +107,7 @@ apstart: ...@@ -107,7 +107,7 @@ apstart:
apstart32: apstart32:
call init32e call init32e
movl $PADDR(apstart64), %edi movl $PADDR(apstart64), %eax
ljmp $KCSEG, $PADDR(tramp64) // code64 segment selector ljmp $KCSEG, $PADDR(tramp64) // code64 segment selector
.code64 .code64
...@@ -124,11 +124,11 @@ apstart64: ...@@ -124,11 +124,11 @@ apstart64:
tramp64: tramp64:
# The linker thinks we are running at tramp64, but we're actually # The linker thinks we are running at tramp64, but we're actually
# running at PADDR(tramp64), so use an explicit calculation to # running at PADDR(tramp64), so use an explicit calculation to
# load and jump to the correct address. %rdi should hold the # load and jump to the correct address. %rax should hold the
# physical address of the jmp target. # physical address of the jmp target.
movq $KBASE, %r11 movq $KBASE, %r11
addq %r11, %rdi addq %r11, %rax
jmp *%rdi jmp *%rax
# Initial stack # Initial stack
.comm stack, STACK .comm stack, STACK
......
...@@ -9,6 +9,11 @@ ...@@ -9,6 +9,11 @@
#include "kalloc.h" #include "kalloc.h"
#include "mtrace.h" #include "mtrace.h"
#include "cpu.h" #include "cpu.h"
#include "multiboot.h"
static struct Mbmem mem[128];
static u64 nmem;
static u64 membytes;
struct kmem kmems[NCPU]; struct kmem kmems[NCPU];
struct kmem kstacks[NCPU]; struct kmem kstacks[NCPU];
...@@ -19,6 +24,77 @@ enum { kalloc_memset = 0 }; ...@@ -19,6 +24,77 @@ enum { kalloc_memset = 0 };
static int kinited __mpalign__; static int kinited __mpalign__;
static struct Mbmem *
memsearch(paddr pa)
{
struct Mbmem *e;
struct Mbmem *q;
q = mem+nmem;
for (e = &mem[0]; e < q; e++)
if ((e->base <= pa) && ((e->base+e->length) > pa))
return e;
return NULL;
}
static u64
memsize(void *va)
{
struct Mbmem *e;
paddr pa = v2p(va);
e = memsearch(pa);
if (e == NULL)
return -1;
return (e->base+e->length) - pa;
}
static void *
memnext(void *va, u64 inc)
{
struct Mbmem *e, *q;
paddr pa = v2p(va);
e = memsearch(pa);
if (e == NULL)
return (void *)-1;
pa += inc;
if (pa < (e->base+e->length))
return p2v(pa);
q = mem+nmem;
for (e = e + 1; e < q; e++)
return p2v(e->base);
return (void *)-1;
}
static void
initmem(u64 mbaddr)
{
struct Mbdata *mb;
struct Mbmem *mbmem;
u8 *p, *ep;
mb = p2v(mbaddr);
if(!(mb->flags & (1<<6)))
panic("multiboot header has no memory map");
p = p2v(mb->mmap_addr);
ep = p + mb->mmap_length;
while (p < ep) {
mbmem = (Mbmem *)(p+4);
p += 4 + *(u32*)p;
if (mbmem->type == 1) {
membytes += mbmem->length;
mem[nmem] = *mbmem;
nmem++;
}
}
}
// simple page allocator to get off the ground during boot // simple page allocator to get off the ground during boot
static char * static char *
pgalloc(void) pgalloc(void)
...@@ -41,7 +117,7 @@ kfree_pool(struct kmem *m, char *v) ...@@ -41,7 +117,7 @@ kfree_pool(struct kmem *m, char *v)
{ {
struct run *r; struct run *r;
if((uptr)v % PGSIZE || v < end || v2p(v) >= PHYSTOP) if((uptr)v % PGSIZE || v < end || memsize(v) == -1ull)
panic("kfree_pool"); panic("kfree_pool");
// Fill with junk to catch dangling refs. // Fill with junk to catch dangling refs.
...@@ -147,10 +223,13 @@ kminit(void) ...@@ -147,10 +223,13 @@ kminit(void)
// Initialize free list of physical pages. // Initialize free list of physical pages.
void void
initkalloc(void) initkalloc(u64 mbaddr)
{ {
char *p; char *p;
char *e; u64 n;
u64 k;
initmem(mbaddr);
for (int c = 0; c < NCPU; c++) { for (int c = 0; c < NCPU; c++) {
kmems[c].name[0] = (char) c + '0'; kmems[c].name[0] = (char) c + '0';
...@@ -166,18 +245,35 @@ initkalloc(void) ...@@ -166,18 +245,35 @@ initkalloc(void)
kstacks[c].size = KSTACKSIZE; kstacks[c].size = KSTACKSIZE;
} }
cprintf("%lu mbytes\n", membytes / (1<<20));
n = membytes / NCPU;
if (n & (PGSIZE-1)) {
cprintf("bytes/CPU isn't aligned\n");
n = PGROUNDDOWN(n);
}
p = (char*)PGROUNDUP((uptr)newend); p = (char*)PGROUNDUP((uptr)newend);
e = (char*)KBASE; k = (((uptr)p) - KBASE);
for (int c = 0; c < NCPU; c++) { for (int c = 0; c < NCPU; c++) {
char *sp = p + CPUKSTACKS*KSTACKSIZE; // Fill the stack allocator
char *ep = e + (PHYSTOP/NCPU); for (int i = 0; i < CPUKSTACKS; i++, k += KSTACKSIZE) {
if (sp >= ep) if (p == (void *)-1)
panic("Too many stacks"); panic("initkalloc: e820next");
for (; p < sp; p += KSTACKSIZE) // XXX(sbw) handle this condition
if (memsize(p) < KSTACKSIZE)
panic("initkalloc: e820size");
kfree_pool(&kstacks[c], p); kfree_pool(&kstacks[c], p);
for (; p < ep; p += PGSIZE) p = memnext(p, KSTACKSIZE);
}
// The rest goes to the page allocator
for (; k != n; k += PGSIZE, p = memnext(p, PGSIZE)) {
if (p == (void *)-1)
panic("initkalloc: e820next");
kfree_pool(&kmems[c], p); kfree_pool(&kmems[c], p);
e = p; }
k = 0;
} }
kminit(); kminit();
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "multiboot.h" #include "multiboot.h"
#include "kernel.h" #include "kernel.h"
#include "cpu.h" #include "cpu.h"
#include "e820.h"
extern void initpic(void); extern void initpic(void);
extern void initioapic(void); extern void initioapic(void);
...@@ -14,7 +15,7 @@ extern void initlapic(void); ...@@ -14,7 +15,7 @@ extern void initlapic(void);
extern void inittls(void); extern void inittls(void);
extern void inittrap(void); extern void inittrap(void);
extern void initseg(void); extern void initseg(void);
extern void initkalloc(void); extern void initkalloc(u64 mbaddr);
extern void initrcu(void); extern void initrcu(void);
extern void initproc(void); extern void initproc(void);
extern void initbio(void); extern void initbio(void);
...@@ -74,7 +75,7 @@ bootothers(void) ...@@ -74,7 +75,7 @@ bootothers(void)
} }
void void
cmain(void) cmain(u64 mbmagic, u64 mbaddr)
{ {
extern pml4e_t kpml4[]; extern pml4e_t kpml4[];
extern u64 cpuhz; extern u64 cpuhz;
...@@ -91,9 +92,8 @@ cmain(void) ...@@ -91,9 +92,8 @@ cmain(void)
inittrap(); inittrap();
initmp(); initmp();
initlapic(); initlapic();
initkalloc(mbaddr);
initkalloc(); initgc(); // gc epochs
initgc(); // initialize rcu module
initproc(); // process table initproc(); // process table
initbio(); // buffer cache initbio(); // buffer cache
initinode(); // inode cache initinode(); // inode cache
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论