提交 83ec933a 创建 作者: Nickolai Zeldovich's avatar Nickolai Zeldovich

-mcmodel=kernel generates somewhat faster and more readable code

上级 d871d353
...@@ -8,14 +8,28 @@ extern "C" { ...@@ -8,14 +8,28 @@ extern "C" {
#include <stdarg.h> #include <stdarg.h>
#define KBASE 0xFFFFFF0000000000ull #define KBASE 0xFFFFFF0000000000ull
#define KCODE 0xFFFFFFFFC0000000ull
#define KSHARED 0xFFFFF00000000000ull #define KSHARED 0xFFFFF00000000000ull
#define USERTOP 0x0000800000000000ull #define USERTOP 0x0000800000000000ull
#define KCSEG (2<<3) /* kernel code segment */ #define KCSEG (2<<3) /* kernel code segment */
#define KDSEG (3<<3) /* kernel data segment */ #define KDSEG (3<<3) /* kernel data segment */
static inline uptr v2p(void *a) { return (uptr) a - KBASE; } static inline uptr v2p(void *a) {
static inline void *p2v(uptr a) { return (u8 *) a + KBASE; } uptr ua = (uptr) a;
if (ua >= KCODE)
return ua - KCODE;
else
return ua - KBASE;
}
static inline void *p2v(uptr a) {
uptr ac = a + KCODE;
if (ac >= KCODE)
return (void*) ac;
else
return (u8 *) a + KBASE;
}
struct trapframe; struct trapframe;
struct cilkframe; struct cilkframe;
......
...@@ -63,8 +63,8 @@ $(O)/kernel/%.o: lib/%.cc ...@@ -63,8 +63,8 @@ $(O)/kernel/%.o: lib/%.cc
$(Q)mkdir -p $(@D) $(Q)mkdir -p $(@D)
$(Q)$(CXX) $(CXXFLAGS) -c -o $@ $< $(Q)$(CXX) $(CXXFLAGS) -c -o $@ $<
$(O)/kernel/%.o: CFLAGS+=-mcmodel=large $(O)/kernel/%.o: CFLAGS+=-mcmodel=kernel
$(O)/kernel/%.o: CXXFLAGS+=-mcmodel=large $(O)/kernel/%.o: CXXFLAGS+=-mcmodel=kernel
$(O)/kernel/incbin.o: ASFLAGS+=-DMAKE_OUT=$(O) $(O)/kernel/incbin.o: ASFLAGS+=-DMAKE_OUT=$(O)
$(O)/kernel/incbin.o: $(O)/kernel/initcode $(O)/kernel/bootother $(O)/fs.img $(O)/kernel/incbin.o: $(O)/kernel/initcode $(O)/kernel/bootother $(O)/fs.img
......
...@@ -6,17 +6,17 @@ ...@@ -6,17 +6,17 @@
#include "mmu.h" #include "mmu.h"
# KADDR is the kernel virtual address of the first byte of physical memory. # KADDR is the kernel virtual address of the first byte of physical memory.
# The linker loads the executable as if starting at KBASE+2MB, but we # The linker loads the executable as if starting at KCODE+2MB, but we
# ask the loader to load the kernel at physical 2MB and then set up the # ask the loader to load the kernel at physical 2MB and then set up the
# necessary memory mapping to switch to the higher address. # necessary memory mapping to switch to the higher address.
# The value of KBASE must match the definitions in kernel.h and kernel.ld. # The value of KCODE must match the definitions in kernel.h and kernel.ld.
#define KBASE 0xFFFFFF0000000000 #define KCODE 0xFFFFFFFFC0000000
# PADDR(x) is the physical memory address corresponding to x. # PADDR(x) is the physical memory address corresponding to x.
# Until we set up the memory map, fairly late in this file, we have to # Until we set up the memory map, fairly late in this file, we have to
# refer to PADDR(symbol) instead of symbol, so that we use the # refer to PADDR(symbol) instead of symbol, so that we use the
# physical address. # physical address.
#define PADDR(x) ((x) - KBASE) #define PADDR(x) ((x) - KCODE)
# STACK is the size of the bootstrap stack. # STACK is the size of the bootstrap stack.
#define STACK 8192 #define STACK 8192
...@@ -126,7 +126,7 @@ tramp64: ...@@ -126,7 +126,7 @@ tramp64:
# 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. %rax 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 $KCODE, %r11
addq %r11, %rax addq %r11, %rax
jmp *%rax jmp *%rax
...@@ -134,7 +134,7 @@ tramp64: ...@@ -134,7 +134,7 @@ tramp64:
.comm stack, STACK .comm stack, STACK
# Page tables. See section 4.5 of 253668.pdf. # Page tables. See section 4.5 of 253668.pdf.
# We map the first GB of physical memory at 0 and at KBASE. At boot # We map the first GB of physical memory at 0 and at KCODE. At boot
# time we are using the mapping at 0 but during ordinary execution we # time we are using the mapping at 0 but during ordinary execution we
# use the high mapping. # use the high mapping.
# The intent is that after bootstrap the kernel can expand this mapping # The intent is that after bootstrap the kernel can expand this mapping
...@@ -145,10 +145,8 @@ tramp64: ...@@ -145,10 +145,8 @@ tramp64:
.global kpml4 .global kpml4
kpml4: kpml4:
.quad PADDR(pdpt0) + (1<<0) + (1<<1) // present, read/write .quad PADDR(pdpt0) + (1<<0) + (1<<1) // present, read/write
.quad 0 .space 4096 - 2*8
.space 4096 - 2*16
.quad PADDR(pdpt1) + (1<<0) + (1<<1) // present, read/write .quad PADDR(pdpt1) + (1<<0) + (1<<1) // present, read/write
.quad 0
.align 4096 .align 4096
pdpt0: pdpt0:
.quad PADDR(pdt) + (1<<0) + (1<<1) // present, read/write .quad PADDR(pdt) + (1<<0) + (1<<1) // present, read/write
...@@ -156,8 +154,8 @@ pdpt0: ...@@ -156,8 +154,8 @@ pdpt0:
.align 4096 .align 4096
pdpt1: pdpt1:
.quad PADDR(pdt) + (1<<0) + (1<<1) // present, read/write
.space 4096 - 8 .space 4096 - 8
.quad PADDR(pdt) + (1<<0) + (1<<1) // present, read/write
.align 4096 .align 4096
pdt: pdt:
......
...@@ -63,7 +63,6 @@ walkpgdir(pgmap *pml4, u64 va, int create) ...@@ -63,7 +63,6 @@ walkpgdir(pgmap *pml4, u64 va, int create)
void void
initpg(void) initpg(void)
{ {
extern char end[];
u64 va = KBASE; u64 va = KBASE;
paddr pa = 0; paddr pa = 0;
...@@ -71,12 +70,9 @@ initpg(void) ...@@ -71,12 +70,9 @@ initpg(void)
auto pdp = descend(&kpml4, va, 0, 1, 3); auto pdp = descend(&kpml4, va, 0, 1, 3);
auto pd = descend(pdp, va, 0, 1, 2); auto pd = descend(pdp, va, 0, 1, 2);
atomic<pme_t> *sp = &pd->e[PX(1,va)]; atomic<pme_t> *sp = &pd->e[PX(1,va)];
u64 flags = PTE_W | PTE_P | PTE_PS; u64 flags = PTE_W | PTE_P | PTE_PS | PTE_NX;
// Set NX for non-code pages
if (va >= (u64) end)
flags |= PTE_NX;
*sp = pa | flags; *sp = pa | flags;
va = va + PGSIZE*512; va += PGSIZE*512;
pa += PGSIZE*512; pa += PGSIZE*512;
} }
} }
......
...@@ -118,8 +118,6 @@ kfree_pool(struct kmem *m, char *v) ...@@ -118,8 +118,6 @@ kfree_pool(struct kmem *m, char *v)
if ((uptr)v % PGSIZE) if ((uptr)v % PGSIZE)
panic("kfree_pool: misaligned %p", v); panic("kfree_pool: misaligned %p", v);
if (v < end)
panic("kfree_pool: less than end %p", v);
if (memsize(v) == -1ull) if (memsize(v) == -1ull)
panic("kfree_pool: unknown region %p", v); panic("kfree_pool: unknown region %p", v);
...@@ -261,7 +259,8 @@ initkalloc(u64 mbaddr) ...@@ -261,7 +259,8 @@ initkalloc(u64 mbaddr)
n = PGROUNDDOWN(n); n = PGROUNDDOWN(n);
p = (char*)PGROUNDUP((uptr)newend); p = (char*)PGROUNDUP((uptr)newend);
k = (((uptr)p) - KBASE); k = (((uptr)p) - KCODE);
p = (char*) KBASE + k;
for (int c = 0; c < NCPU; c++) { for (int c = 0; c < NCPU; c++) {
// Fill slab allocators // Fill slab allocators
strncpy(slabmem[slab_stack][c].name, " kstack", MAXNAME); strncpy(slabmem[slab_stack][c].name, " kstack", MAXNAME);
...@@ -317,7 +316,7 @@ verifyfree(char *ptr, u64 nbytes) ...@@ -317,7 +316,7 @@ verifyfree(char *ptr, u64 nbytes)
for (; p < e; p++) { for (; p < e; p++) {
// Search for pointers in the ptr region // Search for pointers in the ptr region
u64 x = *(uptr *)p; u64 x = *(uptr *)p;
if (KBASE < x && x < KBASE+(128ull<<30)) { if ((KBASE < x && x < KBASE+(128ull<<30)) || (KCODE < x)) {
struct klockstat *kls = (struct klockstat *) x; struct klockstat *kls = (struct klockstat *) x;
if (kls->magic == LOCKSTAT_MAGIC) if (kls->magic == LOCKSTAT_MAGIC)
panic("LOCKSTAT_MAGIC %p(%lu):%p->%p", panic("LOCKSTAT_MAGIC %p(%lu):%p->%p",
......
...@@ -4,7 +4,7 @@ ENTRY(xxx) ...@@ -4,7 +4,7 @@ ENTRY(xxx)
SECTIONS SECTIONS
{ {
. = 0xFFFFFF0000100000; . = 0xFFFFFFFFC0100000;
PROVIDE(text = .); PROVIDE(text = .);
.text : AT(0x100000) { .text : AT(0x100000) {
*(.text .stub .text.* .gnu.linkonce.t.*) *(.text .stub .text.* .gnu.linkonce.t.*)
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论