提交 7a53b299 创建 作者: Nickolai Zeldovich's avatar Nickolai Zeldovich

compile vm code as c++

上级 15c7f34b
...@@ -14,16 +14,19 @@ HW ?= qemu ...@@ -14,16 +14,19 @@ HW ?= qemu
O = o.$(HW) O = o.$(HW)
CC = $(TOOLPREFIX)gcc CC = $(TOOLPREFIX)gcc
CXX = $(TOOLPREFIX)g++
AS = $(TOOLPREFIX)gas AS = $(TOOLPREFIX)gas
LD = $(TOOLPREFIX)ld LD = $(TOOLPREFIX)ld
NM = $(TOOLPREFIX)nm NM = $(TOOLPREFIX)nm
OBJCOPY = $(TOOLPREFIX)objcopy OBJCOPY = $(TOOLPREFIX)objcopy
CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb \ COMFLAGS := -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall \
-m64 -Werror -std=c99 -fms-extensions -mno-sse -mcmodel=large -mno-red-zone \ -MD -ggdb -m64 -Werror -fms-extensions -mno-sse \
-I$(QEMUSRC) \ -mcmodel=large -mno-red-zone -I$(QEMUSRC) -fno-omit-frame-pointer \
-fno-omit-frame-pointer -DHW_$(HW) -include param.h -include compiler.h -DHW_$(HW) -include param.h -include compiler.h
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector) COMFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
CFLAGS := $(COMFLAGS) -std=c99
CXXFLAGS := $(COMFLAGS) -std=c++0x
ASFLAGS = -m64 -gdwarf-2 -MD ASFLAGS = -m64 -gdwarf-2 -MD
LDFLAGS += -m elf_x86_64 LDFLAGS += -m elf_x86_64
...@@ -112,6 +115,10 @@ $(O)/%.o: %.c ...@@ -112,6 +115,10 @@ $(O)/%.o: %.c
@echo " CC $@" @echo " CC $@"
$(Q)$(CC) $(CFLAGS) -c -o $@ $< $(Q)$(CC) $(CFLAGS) -c -o $@ $<
$(O)/%.o: %.cc
@echo " CXX $@"
$(Q)$(CXX) $(CXXFLAGS) -c -o $@ $<
$(O)/incbin.o: ASFLAGS+=-DMAKE_OUT=$(O) $(O)/incbin.o: ASFLAGS+=-DMAKE_OUT=$(O)
$(O)/incbin.o: $(O)/initcode $(O)/bootother $(O)/fs.img $(O)/incbin.o: $(O)/initcode $(O)/bootother $(O)/fs.img
$(O)/%.o: %.S $(O)/%.o: %.S
......
...@@ -77,7 +77,7 @@ writebuf(int c, void *arg) ...@@ -77,7 +77,7 @@ writebuf(int c, void *arg)
} }
void void
vsnprintf(char *buf, u32 n, char *fmt, va_list ap) vsnprintf(char *buf, u32 n, const char *fmt, va_list ap)
{ {
struct bufstate bs = { buf, buf+n-1 }; struct bufstate bs = { buf, buf+n-1 };
vprintfmt(writebuf, (void*) &bs, fmt, ap); vprintfmt(writebuf, (void*) &bs, fmt, ap);
...@@ -85,7 +85,7 @@ vsnprintf(char *buf, u32 n, char *fmt, va_list ap) ...@@ -85,7 +85,7 @@ vsnprintf(char *buf, u32 n, char *fmt, va_list ap)
} }
void void
snprintf(char *buf, u32 n, char *fmt, ...) snprintf(char *buf, u32 n, const char *fmt, ...)
{ {
va_list ap; va_list ap;
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#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) { return (uptr) a - KBASE; }
static inline void *p2v(uptr a) { return (void *) a + KBASE; } static inline void *p2v(uptr a) { return (u8 *) a + KBASE; }
struct trapframe; struct trapframe;
struct cilkframe; struct cilkframe;
...@@ -48,7 +48,7 @@ void cprintf(const char*, ...) __attribute__((format(printf, 1, 2))); ...@@ -48,7 +48,7 @@ void cprintf(const char*, ...) __attribute__((format(printf, 1, 2)));
void panic(const char*, ...) void panic(const char*, ...)
__noret__ __attribute__((format(printf, 1, 2))); __noret__ __attribute__((format(printf, 1, 2)));
void kerneltrap(struct trapframe *tf) __noret__; void kerneltrap(struct trapframe *tf) __noret__;
void snprintf(char *buf, u32 n, char *fmt, ...); void snprintf(char *buf, u32 n, const char *fmt, ...);
void consoleintr(int(*)(void)); void consoleintr(int(*)(void));
#define assert(c) if (!(c)) { cprintf("%s:%d: ", __FILE__, __LINE__); panic("assertion failure"); } #define assert(c) if (!(c)) { cprintf("%s:%d: ", __FILE__, __LINE__); panic("assertion failure"); }
...@@ -307,8 +307,10 @@ void uartputc(char c); ...@@ -307,8 +307,10 @@ void uartputc(char c);
void uartintr(void); void uartintr(void);
// vm.c // vm.c
enum vmntype { EAGER, ONDEMAND };
struct vmap * vmap_alloc(void); struct vmap * vmap_alloc(void);
struct vmnode* vmn_alloc(u64, u32); struct vmnode* vmn_alloc(u64, enum vmntype);
struct vmnode* vmn_allocpg(u64); struct vmnode* vmn_allocpg(u64);
int vmap_insert(struct vmap*, struct vmnode *, uptr); int vmap_insert(struct vmap*, struct vmnode *, uptr);
struct vma * vmap_lookup(struct vmap*, uptr, uptr); struct vma * vmap_lookup(struct vmap*, uptr, uptr);
......
...@@ -127,7 +127,7 @@ writebuf(void *arg, char c) ...@@ -127,7 +127,7 @@ writebuf(void *arg, char c)
} }
void void
vsnprintf(char *buf, u32 n, char *fmt, va_list ap) vsnprintf(char *buf, u32 n, const char *fmt, va_list ap)
{ {
struct bufstate bs = { buf, buf+n-1 }; struct bufstate bs = { buf, buf+n-1 };
vprintfmt(writebuf, (void*) &bs, fmt, ap); vprintfmt(writebuf, (void*) &bs, fmt, ap);
...@@ -135,7 +135,7 @@ vsnprintf(char *buf, u32 n, char *fmt, va_list ap) ...@@ -135,7 +135,7 @@ vsnprintf(char *buf, u32 n, char *fmt, va_list ap)
} }
void void
snprintf(char *buf, u32 n, char *fmt, ...) snprintf(char *buf, u32 n, const char *fmt, ...)
{ {
va_list ap; va_list ap;
......
...@@ -46,5 +46,5 @@ int forkt(void *sp, void *pc, void *arg); ...@@ -46,5 +46,5 @@ int forkt(void *sp, void *pc, void *arg);
// printf.c // printf.c
void printf(int, char*, ...); void printf(int, char*, ...);
void snprintf(char *buf, unsigned int n, char *fmt, ...); void snprintf(char *buf, unsigned int n, const char *fmt, ...);
void die(const char* errstr, ...) __attribute__((noreturn)); void die(const char* errstr, ...) __attribute__((noreturn));
extern "C" {
#include "types.h" #include "types.h"
#include "amd64.h" #include "amd64.h"
#include "mmu.h" #include "mmu.h"
...@@ -10,6 +11,7 @@ ...@@ -10,6 +11,7 @@
#include "condvar.h" #include "condvar.h"
#include "proc.h" #include "proc.h"
#include "vm.h" #include "vm.h"
}
static void vmap_free(void *p); static void vmap_free(void *p);
...@@ -18,7 +20,7 @@ enum { vm_debug = 0 }; ...@@ -18,7 +20,7 @@ enum { vm_debug = 0 };
static struct vma * static struct vma *
vma_alloc(void) vma_alloc(void)
{ {
struct vma *e = kmalloc(sizeof(struct vma)); struct vma *e = (struct vma *) kmalloc(sizeof(struct vma));
if (e == 0) if (e == 0)
return 0; return 0;
memset(e, 0, sizeof(struct vma)); memset(e, 0, sizeof(struct vma));
...@@ -103,7 +105,7 @@ vmap_decref(struct vmap *m) ...@@ -103,7 +105,7 @@ vmap_decref(struct vmap *m)
struct vmap * struct vmap *
vmap_alloc(void) vmap_alloc(void)
{ {
struct vmap *m = kmalloc(sizeof(struct vmap)); struct vmap *m = (struct vmap *) kmalloc(sizeof(struct vmap));
if (m == 0) if (m == 0)
return 0; return 0;
memset(m, 0, sizeof(struct vmap)); memset(m, 0, sizeof(struct vmap));
...@@ -130,7 +132,7 @@ vmn_doload(struct vmnode *vmn, struct inode *ip, u64 offset, u64 sz) ...@@ -130,7 +132,7 @@ vmn_doload(struct vmnode *vmn, struct inode *ip, u64 offset, u64 sz)
{ {
for(u64 i = 0; i < sz; i += PGSIZE){ for(u64 i = 0; i < sz; i += PGSIZE){
char *p = vmn->page[i / PGSIZE]; char *p = vmn->page[i / PGSIZE];
u64 n; s64 n;
if(sz - i < PGSIZE) if(sz - i < PGSIZE)
n = sz - i; n = sz - i;
else else
...@@ -295,7 +297,7 @@ copyout(struct vmap *vmap, uptr va, void *p, u64 len) ...@@ -295,7 +297,7 @@ copyout(struct vmap *vmap, uptr va, void *p, u64 len)
struct vmnode * struct vmnode *
vmn_alloc(u64 npg, enum vmntype type) vmn_alloc(u64 npg, enum vmntype type)
{ {
struct vmnode *n = kmalloc(sizeof(struct vmnode)); struct vmnode *n = (struct vmnode *) kmalloc(sizeof(struct vmnode));
if (n == 0) { if (n == 0) {
cprintf("out of vmnodes"); cprintf("out of vmnodes");
return 0; return 0;
...@@ -541,7 +543,7 @@ vmap_copy(struct vmap *m, int share) ...@@ -541,7 +543,7 @@ vmap_copy(struct vmap *m, int share)
return 0; return 0;
acquire(&m->lock); acquire(&m->lock);
for(int i = 0; i < NELEM(m->e); i++) { for(u32 i = 0; i < NELEM(m->e); i++) {
if(m->e[i] == 0) if(m->e[i] == 0)
continue; continue;
c->e[i] = vma_alloc(); c->e[i] = vma_alloc();
...@@ -583,7 +585,7 @@ vmap_remove(struct vmap *m, uptr va_start, u64 len) ...@@ -583,7 +585,7 @@ vmap_remove(struct vmap *m, uptr va_start, u64 len)
{ {
acquire(&m->lock); acquire(&m->lock);
uptr va_end = va_start + len; uptr va_end = va_start + len;
for(int i = 0; i < NELEM(m->e); i++) { for(u32 i = 0; i < NELEM(m->e); i++) {
if(m->e[i] && (m->e[i]->va_start < va_end && m->e[i]->va_end > va_start)) { if(m->e[i] && (m->e[i]->va_start < va_end && m->e[i]->va_end > va_start)) {
if(m->e[i]->va_start != va_start || m->e[i]->va_end != va_end) { if(m->e[i]->va_start != va_start || m->e[i]->va_end != va_end) {
release(&m->lock); release(&m->lock);
......
...@@ -13,7 +13,6 @@ struct vma { ...@@ -13,7 +13,6 @@ struct vma {
}; };
// A memory object (physical pages or inode). // A memory object (physical pages or inode).
enum vmntype { EAGER, ONDEMAND};
struct vmnode { struct vmnode {
u64 npages; u64 npages;
char *page[128]; char *page[128];
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论