diff --git a/defs.h b/defs.h index 262da72..0e8cb52 100644 --- a/defs.h +++ b/defs.h @@ -179,7 +179,6 @@ int uartgetc(void); // vm.c void kvminit(void); -void kvmswitch(void); pagetable_t uvmcreate(void); void uvminit(pagetable_t, char *, uint); uint64 uvmalloc(pagetable_t, uint64, uint64); diff --git a/kalloc.c b/kalloc.c index cfbbae4..1ed1c49 100644 --- a/kalloc.c +++ b/kalloc.c @@ -27,8 +27,6 @@ void kinit() { initlock(&kmem.lock, "kmem"); - if(PHYSTOP > RAMDISK) - panic("kinit"); freerange(end, (void*)PHYSTOP); } @@ -76,7 +74,7 @@ kalloc(void) if(r) kmem.freelist = r->next; release(&kmem.lock); - memset((char*)r, 5, PGSIZE); // fill with junk + if(r) + memset((char*)r, 5, PGSIZE); // fill with junk return (void*)r; } - diff --git a/kernel.ld b/kernel.ld index 08fc280..53c9b90 100644 --- a/kernel.ld +++ b/kernel.ld @@ -26,8 +26,6 @@ SECTIONS } bss : { *(.bss) + PROVIDE(end = .); } - - . = ALIGN(0x1000); - PROVIDE(end = .); } diff --git a/memlayout.h b/memlayout.h index 1a6b200..db233f7 100644 --- a/memlayout.h +++ b/memlayout.h @@ -2,6 +2,7 @@ // qemu -machine virt is set up like this: // 00001000 -- boot ROM, provided by qemu +// 0C000000 -- PLIC // 10000000 -- uart0 registers // 80000000 -- boot ROM jumps here in machine mode // -kernel loads the kernel here @@ -26,7 +27,7 @@ // for use by the kernel and user pages // from physical address 0x80000000 to PHYSTOP. #define KERNBASE 0x80000000L -#define PHYSTOP (KERNBASE + 64*1024*1024) +#define PHYSTOP (KERNBASE + 128*1024*1024) // map the trampoline page to the highest address, // in both user and kernel space. diff --git a/proc.c b/proc.c index e9aec5d..b696d0c 100644 --- a/proc.c +++ b/proc.c @@ -360,7 +360,7 @@ scheduler(void) for(;;){ // Enable interrupts on this processor. intr_on(); - + // Loop over process table looking for process to run. acquire(&ptable.lock); for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ diff --git a/riscv.h b/riscv.h index 6ecee84..d59503c 100644 --- a/riscv.h +++ b/riscv.h @@ -132,6 +132,14 @@ w_stvec(uint64 x) asm("csrw stvec, %0" : : "r" (x)); } +static inline uint64 +r_stvec() +{ + uint64 x; + asm("csrr %0, stvec" : "=r" (x) ); + return x; +} + // use riscv's sv39 page table scheme. #define SATP_SV39 (8L << 60) @@ -201,6 +209,14 @@ intr_get() return (x & SSTATUS_SIE) != 0; } +static inline uint64 +r_sp() +{ + uint64 x; + asm("mv %0, sp" : "=r" (x) ); + return x; +} + #define PGSIZE 4096 // bytes per page #define PGSHIFT 12 // bits of offset within a page diff --git a/start.c b/start.c index 5167ab4..b7af38a 100644 --- a/start.c +++ b/start.c @@ -6,7 +6,7 @@ void main(); // entry.S uses this as the initial stack. -char stack0[4096]; +__attribute__ ((aligned (16))) char stack0[4096]; // entry.S jumps here in machine mode on stack0. void diff --git a/sysproc.c b/sysproc.c index 329feee..82ad884 100644 --- a/sysproc.c +++ b/sysproc.c @@ -39,6 +39,7 @@ sys_sbrk(void) if(argint(0, &n) < 0) return -1; + printf("sbrk(%d), sz was %d\n", n, (int)myproc()->sz); addr = myproc()->sz; if(growproc(n) < 0) return -1; diff --git a/trap.c b/trap.c index 98ab143..39ff61d 100644 --- a/trap.c +++ b/trap.c @@ -108,7 +108,7 @@ usertrapret(void) // interrupts and exceptions from kernel code go here, // on whatever the current kernel stack is. // must be 4-byte aligned to fit in stvec. -void __attribute__ ((aligned (4))) +void kerneltrap() { uint64 sstatus = r_sstatus(); diff --git a/uart.c b/uart.c index 29f6df4..35fac1b 100644 --- a/uart.c +++ b/uart.c @@ -46,6 +46,9 @@ uartinit(void) void uartputc(int c) { + // wait for Transmit Holding Empty to be set in LSR. + while((*R(5) & (1 << 5)) == 0) + ; *R(0) = c; } diff --git a/vm.c b/vm.c index 89b1aa2..1e014a0 100644 --- a/vm.c +++ b/vm.c @@ -33,12 +33,11 @@ kvminit() // PLIC mappages(kernel_pagetable, PLIC, 0x4000000, PLIC, PTE_R | PTE_W); - // map kernel text executable and read-only. mappages(kernel_pagetable, KERNBASE, (uint64)etext-KERNBASE, KERNBASE, PTE_R | PTE_X); - + // map kernel data and the physical RAM we'll make use of. mappages(kernel_pagetable, (uint64)etext, PHYSTOP-(uint64)etext, (uint64)etext, PTE_R | PTE_W); @@ -52,14 +51,8 @@ kvminit() mappages(kernel_pagetable, TRAMPOLINE, PGSIZE, (uint64)trampout, PTE_R | PTE_X); - kvmswitch(); -} - -// Switch h/w page table register to the kernel's page table, -// and enable paging. -void -kvmswitch(void) -{ + // Switch h/w page table register to the kernel's page table, + // and enable paging. w_satp(MAKE_SATP(kernel_pagetable)); } @@ -214,7 +207,7 @@ uvmalloc(pagetable_t pagetable, uint64 oldsz, uint64 newsz) for(; a < newsz; a += PGSIZE){ mem = kalloc(); if(mem == 0){ - uvmdealloc(pagetable, newsz, oldsz); + uvmdealloc(pagetable, a, oldsz); return 0; } memset(mem, 0, PGSIZE);