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

c++ userspace, half-way

上级 e899cb3b
extern "C" {
#include "types.h" #include "types.h"
#include "stat.h" #include "stat.h"
#include "user.h" #include "user.h"
}
char buf[512]; char buf[512];
......
extern "C" {
#include "types.h" #include "types.h"
#include "stat.h" #include "stat.h"
#include "user.h" #include "user.h"
#include "mtrace.h" #include "mtrace.h"
#include "amd64.h" #include "amd64.h"
#include "fcntl.h" #include "fcntl.h"
}
enum { nthread = 2 }; enum { nthread = 2 };
enum { nloop = 100 }; enum { nloop = 100 };
......
extern "C" {
#include "types.h" #include "types.h"
#include "stat.h" #include "stat.h"
#include "user.h" #include "user.h"
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
......
extern "C" {
#include "types.h" #include "types.h"
#include "stat.h" #include "stat.h"
#include "user.h" #include "user.h"
#include "mtrace.h" #include "mtrace.h"
#include "amd64.h" #include "amd64.h"
}
#define NITERS 16 #define NITERS 16
...@@ -18,7 +20,7 @@ execbench(void) ...@@ -18,7 +20,7 @@ execbench(void)
exit(); exit();
} }
if (pid == 0) { if (pid == 0) {
char *av[] = { "forkexecbench", "x", 0 }; const char *av[] = { "forkexecbench", "x", 0 };
exec("forkexecbench", av); exec("forkexecbench", av);
printf(1, "exec failed\n"); printf(1, "exec failed\n");
exit(); exit();
......
extern "C" {
#include "types.h" #include "types.h"
#include "stat.h" #include "stat.h"
#include "user.h" #include "user.h"
#include "mtrace.h" #include "mtrace.h"
}
#define NCHILD 2 #define NCHILD 2
#define NDEPTH 7 #define NDEPTH 7
...@@ -28,7 +30,7 @@ forktree(int depth) ...@@ -28,7 +30,7 @@ forktree(int depth)
depth++; depth++;
char depthbuf[16]; char depthbuf[16];
snprintf(depthbuf, sizeof(depthbuf), "%d", depth); snprintf(depthbuf, sizeof(depthbuf), "%d", depth);
char *av[] = { "forkexectree", depthbuf }; const char *av[] = { "forkexectree", depthbuf };
exec("forkexectree", av); exec("forkexectree", av);
} }
} }
......
extern "C" {
#include "types.h" #include "types.h"
#include "stat.h" #include "stat.h"
#include "user.h" #include "user.h"
#include "mtrace.h" #include "mtrace.h"
}
#define NCHILD 2 #define NCHILD 2
#define NDEPTH 5 #define NDEPTH 5
......
extern "C" {
#include "types.h" #include "types.h"
#include "user.h" #include "user.h"
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
......
extern "C" {
#include "types.h" #include "types.h"
#include "user.h" #include "user.h"
#include "lib.h" #include "lib.h"
#include "unet.h" #include "unet.h"
#include "fcntl.h" #include "fcntl.h"
#include "stat.h" #include "stat.h"
}
#define VERSION "0.1" #define VERSION "0.1"
#define HTTP_VERSION "1.0" #define HTTP_VERSION "1.0"
...@@ -19,7 +21,7 @@ static int xwrite(int fd, const void *buf, u64 n) ...@@ -19,7 +21,7 @@ static int xwrite(int fd, const void *buf, u64 n)
printf(1, "xwrite: failed %d\n", r); printf(1, "xwrite: failed %d\n", r);
return -1; return -1;
} }
buf += r; buf = (char *) buf + r;
n -= r; n -= r;
} }
...@@ -208,7 +210,7 @@ parse(const char *b, char **rurl) ...@@ -208,7 +210,7 @@ parse(const char *b, char **rurl)
b++; b++;
len = b - url; len = b - url;
r = malloc(len+1); r = (char *) malloc(len+1);
if (r == NULL) if (r == NULL)
return -1; return -1;
memmove(r, url, len); memmove(r, url, len);
......
// init: The initial user-level program // init: The initial user-level program
extern "C" {
#include "types.h" #include "types.h"
#include "stat.h" #include "stat.h"
#include "user.h" #include "user.h"
#include "fcntl.h" #include "fcntl.h"
#include "lib.h" #include "lib.h"
}
static char *sh_argv[] = { "sh", 0 }; static const char *sh_argv[] = { "sh", 0 };
static char *app_argv[][MAXARG] = { static const char *app_argv[][MAXARG] = {
#ifdef LWIP #ifdef LWIP
{ "telnetd", 0 }, { "telnetd", 0 },
{ "httpd", 0 }, { "httpd", 0 },
...@@ -15,7 +17,7 @@ static char *app_argv[][MAXARG] = { ...@@ -15,7 +17,7 @@ static char *app_argv[][MAXARG] = {
}; };
static int static int
startone(char **argv) startone(const char **argv)
{ {
int pid; int pid;
...@@ -52,7 +54,7 @@ main(void) ...@@ -52,7 +54,7 @@ main(void)
if (mknod("/dev/lockstat", 4, 1) < 0) if (mknod("/dev/lockstat", 4, 1) < 0)
printf(2, "init: mknod lockstat failed\n"); printf(2, "init: mknod lockstat failed\n");
for (int i = 0; i < NELEM(app_argv); i++) for (u32 i = 0; i < NELEM(app_argv); i++)
startone(app_argv[i]); startone(app_argv[i]);
for(;;){ for(;;){
......
extern "C" {
#include "types.h" #include "types.h"
#include "stat.h" #include "stat.h"
#include "user.h" #include "user.h"
}
int int
main(int argc, char **argv) main(int argc, char **argv)
......
extern "C" {
#include "types.h" #include "types.h"
#include "stat.h" #include "stat.h"
#include "user.h" #include "user.h"
#include "fcntl.h" #include "fcntl.h"
#include "amd64.h" #include "amd64.h"
#include "lockstat.h" #include "lockstat.h"
}
static void static void
xwrite(int fd, char c) xwrite(int fd, char c)
...@@ -50,7 +52,7 @@ stats(void) ...@@ -50,7 +52,7 @@ stats(void)
} }
int int
main(int ac, char *av[]) main(int ac, const char *av[])
{ {
int fd = open("/dev/lockstat", O_RDWR); int fd = open("/dev/lockstat", O_RDWR);
if (fd < 0) if (fd < 0)
......
extern "C" {
#include "types.h" #include "types.h"
#include "stat.h" #include "stat.h"
#include "user.h" #include "user.h"
#include "mtrace.h" #include "mtrace.h"
#include "amd64.h" #include "amd64.h"
#include "uspinlock.h" #include "uspinlock.h"
}
static struct uspinlock l; static struct uspinlock l;
static volatile int tcount; static volatile int tcount;
...@@ -57,7 +59,7 @@ main(int ac, char **av) ...@@ -57,7 +59,7 @@ main(int ac, char **av)
sbrk(8192); sbrk(8192);
void *tstack = sbrk(0); void *tstack = sbrk(0);
// printf(1, "tstack %lx\n", tstack); // printf(1, "tstack %lx\n", tstack);
int tid = forkt(tstack, thr, (void *)(u64)i); int tid = forkt(tstack, (void*) thr, (void *)(u64)i);
if (0) printf(1, "mapbench[%d]: child %d\n", getpid(), tid); if (0) printf(1, "mapbench[%d]: child %d\n", getpid(), tid);
} }
......
...@@ -13,7 +13,8 @@ UPROGS += \ ...@@ -13,7 +13,8 @@ UPROGS += \
$(O)/_telnetd \ $(O)/_telnetd \
$(O)/_httpd $(O)/_httpd
CFLAGS += -Ilwip/src/include -Inet -Ilwip/src/include/ipv4 -I. -DLWIP CFLAGS += -Ilwip/src/include -Inet -Ilwip/src/include/ipv4 -I. -DLWIP
CXXFLAGS += -Ilwip/src/include -Inet -Ilwip/src/include/ipv4 -I. -DLWIP
LWIP_CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb \ LWIP_CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb \
-m64 -Werror -std=c99 -fms-extensions -mno-sse -mcmodel=large -I$(QEMUSRC) \ -m64 -Werror -std=c99 -fms-extensions -mno-sse -mcmodel=large -I$(QEMUSRC) \
......
extern "C" {
#include "types.h" #include "types.h"
#include "stat.h" #include "stat.h"
#include "user.h" #include "user.h"
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
......
// Shell. // Shell.
extern "C" {
#include "types.h" #include "types.h"
#include "user.h" #include "user.h"
#include "fcntl.h" #include "fcntl.h"
}
// Parsed command representation // Parsed command representation
#define EXEC 1 #define EXEC 1
...@@ -19,7 +21,7 @@ struct cmd { ...@@ -19,7 +21,7 @@ struct cmd {
struct execcmd { struct execcmd {
int type; int type;
char *argv[MAXARGS]; const char *argv[MAXARGS];
char *eargv[MAXARGS]; char *eargv[MAXARGS];
}; };
...@@ -50,7 +52,7 @@ struct backcmd { ...@@ -50,7 +52,7 @@ struct backcmd {
}; };
int fork1(void); // Fork but panics on failure. int fork1(void); // Fork but panics on failure.
void panic(char*); void panic(const char*);
struct cmd *parsecmd(char*); struct cmd *parsecmd(char*);
// Execute cmd. Never returns. // Execute cmd. Never returns.
...@@ -173,7 +175,7 @@ main(void) ...@@ -173,7 +175,7 @@ main(void)
} }
void void
panic(char *s) panic(const char *s)
{ {
printf(2, "%s\n", s); printf(2, "%s\n", s);
exit(); exit();
...@@ -198,7 +200,7 @@ execcmd(void) ...@@ -198,7 +200,7 @@ execcmd(void)
{ {
struct execcmd *cmd; struct execcmd *cmd;
cmd = malloc(sizeof(*cmd)); cmd = (struct execcmd *) malloc(sizeof(*cmd));
memset(cmd, 0, sizeof(*cmd)); memset(cmd, 0, sizeof(*cmd));
cmd->type = EXEC; cmd->type = EXEC;
return (struct cmd*)cmd; return (struct cmd*)cmd;
...@@ -209,7 +211,7 @@ redircmd(struct cmd *subcmd, char *file, char *efile, int mode, int fd) ...@@ -209,7 +211,7 @@ redircmd(struct cmd *subcmd, char *file, char *efile, int mode, int fd)
{ {
struct redircmd *cmd; struct redircmd *cmd;
cmd = malloc(sizeof(*cmd)); cmd = (struct redircmd *) malloc(sizeof(*cmd));
memset(cmd, 0, sizeof(*cmd)); memset(cmd, 0, sizeof(*cmd));
cmd->type = REDIR; cmd->type = REDIR;
cmd->cmd = subcmd; cmd->cmd = subcmd;
...@@ -225,7 +227,7 @@ pipecmd(struct cmd *left, struct cmd *right) ...@@ -225,7 +227,7 @@ pipecmd(struct cmd *left, struct cmd *right)
{ {
struct pipecmd *cmd; struct pipecmd *cmd;
cmd = malloc(sizeof(*cmd)); cmd = (struct pipecmd *) malloc(sizeof(*cmd));
memset(cmd, 0, sizeof(*cmd)); memset(cmd, 0, sizeof(*cmd));
cmd->type = PIPE; cmd->type = PIPE;
cmd->left = left; cmd->left = left;
...@@ -238,7 +240,7 @@ listcmd(struct cmd *left, struct cmd *right) ...@@ -238,7 +240,7 @@ listcmd(struct cmd *left, struct cmd *right)
{ {
struct listcmd *cmd; struct listcmd *cmd;
cmd = malloc(sizeof(*cmd)); cmd = (struct listcmd *) malloc(sizeof(*cmd));
memset(cmd, 0, sizeof(*cmd)); memset(cmd, 0, sizeof(*cmd));
cmd->type = LIST; cmd->type = LIST;
cmd->left = left; cmd->left = left;
...@@ -251,7 +253,7 @@ backcmd(struct cmd *subcmd) ...@@ -251,7 +253,7 @@ backcmd(struct cmd *subcmd)
{ {
struct backcmd *cmd; struct backcmd *cmd;
cmd = malloc(sizeof(*cmd)); cmd = (struct backcmd *) malloc(sizeof(*cmd));
memset(cmd, 0, sizeof(*cmd)); memset(cmd, 0, sizeof(*cmd));
cmd->type = BACK; cmd->type = BACK;
cmd->cmd = subcmd; cmd->cmd = subcmd;
...@@ -309,7 +311,7 @@ gettoken(char **ps, char *es, char **q, char **eq) ...@@ -309,7 +311,7 @@ gettoken(char **ps, char *es, char **q, char **eq)
} }
int int
peek(char **ps, char *es, char *toks) peek(char **ps, char *es, const char *toks)
{ {
char *s; char *s;
......
extern "C" {
#include "types.h" #include "types.h"
#include "stat.h" #include "stat.h"
#include "user.h" #include "user.h"
#include "fcntl.h" #include "fcntl.h"
#include "amd64.h" #include "amd64.h"
}
int int
main(int ac, char *av[]) main(int ac, char *av[])
......
extern "C" {
#include "types.h" #include "types.h"
#include "user.h" #include "user.h"
#include "unet.h" #include "unet.h"
}
int int
main(void) main(void)
...@@ -46,7 +48,7 @@ main(void) ...@@ -46,7 +48,7 @@ main(void)
continue; continue;
} }
if (pid == 0) { if (pid == 0) {
static char *argv[] = { "sh", 0 }; static const char *argv[] = { "sh", 0 };
close(0); close(0);
close(1); close(1);
close(2); close(2);
......
extern "C" {
#include "types.h" #include "types.h"
#include "stat.h" #include "stat.h"
#include "user.h" #include "user.h"
#include "mtrace.h" #include "mtrace.h"
#include "amd64.h" #include "amd64.h"
#include "uspinlock.h" #include "uspinlock.h"
}
static struct uspinlock l; static struct uspinlock l;
static volatile int tcount; static volatile int tcount;
...@@ -28,7 +30,7 @@ main(void) ...@@ -28,7 +30,7 @@ main(void)
for(int i = 0; i < nthread; i++) { for(int i = 0; i < nthread; i++) {
sbrk(8192); sbrk(8192);
void *tstack = sbrk(0); void *tstack = sbrk(0);
int tid = forkt(tstack, thr, (void*)(u64)(0xc0ffee00|i)); int tid = forkt(tstack, (void*) thr, (void*)(u64)(0xc0ffee00|i));
printf(1, "thrtest[%d]: child %d\n", getpid(), tid); printf(1, "thrtest[%d]: child %d\n", getpid(), tid);
} }
......
extern "C" {
#include "types.h" #include "types.h"
#include "stat.h" #include "stat.h"
#include "user.h" #include "user.h"
#include "fcntl.h" #include "fcntl.h"
#include "amd64.h" #include "amd64.h"
}
int int
main(int ac, char *av[]) main(int ac, const char *av[])
{ {
u64 t0 = rdtsc(); u64 t0 = rdtsc();
......
...@@ -9,14 +9,14 @@ int write(int, const void*, int); ...@@ -9,14 +9,14 @@ int write(int, const void*, int);
int read(int, void*, int); int read(int, void*, int);
int close(int); int close(int);
int kill(int); int kill(int);
int exec(char*, char**); int exec(const char*, const char**);
int open(const char*, int); int open(const char*, int);
int mknod(char*, short, short); int mknod(const char*, short, short);
int unlink(char*); int unlink(const char*);
int fstat(int fd, struct stat*); int fstat(int fd, struct stat*);
int link(char*, char*); int link(const char*, const char*);
int mkdir(char*); int mkdir(const char*);
int chdir(char*); int chdir(const char*);
int dup(int); int dup(int);
int getpid(void); int getpid(void);
char* sbrk(int); char* sbrk(int);
......
extern "C" {
#include "types.h" #include "types.h"
#include "stat.h" #include "stat.h"
#include "user.h" #include "user.h"
...@@ -5,10 +6,11 @@ ...@@ -5,10 +6,11 @@
#include "fcntl.h" #include "fcntl.h"
#include "syscall.h" #include "syscall.h"
#include "traps.h" #include "traps.h"
}
char buf[2048]; char buf[2048];
char name[3]; char name[3];
char *echoargv[] = { "echo", "ALL", "TESTS", "PASSED", 0 }; const char *echoargv[] = { "echo", "ALL", "TESTS", "PASSED", 0 };
int stdout = 1; int stdout = 1;
// simple file system tests // simple file system tests
...@@ -85,7 +87,7 @@ writetest(void) ...@@ -85,7 +87,7 @@ writetest(void)
void void
writetest1(void) writetest1(void)
{ {
int i, fd, n; int fd, n;
printf(stdout, "big files test\n"); printf(stdout, "big files test\n");
...@@ -95,7 +97,7 @@ writetest1(void) ...@@ -95,7 +97,7 @@ writetest1(void)
exit(); exit();
} }
for(i = 0; i < MAXFILE; i++){ for(u32 i = 0; i < MAXFILE; i++){
((int*)buf)[0] = i; ((int*)buf)[0] = i;
if(write(fd, buf, 512) != 512){ if(write(fd, buf, 512) != 512){
printf(stdout, "error: write big file failed\n", i); printf(stdout, "error: write big file failed\n", i);
...@@ -113,7 +115,7 @@ writetest1(void) ...@@ -113,7 +115,7 @@ writetest1(void)
n = 0; n = 0;
for(;;){ for(;;){
i = read(fd, buf, 512); u32 i = read(fd, buf, 512);
if(i == 0){ if(i == 0){
if(n == MAXFILE - 1){ if(n == MAXFILE - 1){
printf(stdout, "read only %d blocks from big", n); printf(stdout, "read only %d blocks from big", n);
...@@ -331,7 +333,7 @@ mem(void) ...@@ -331,7 +333,7 @@ mem(void)
if((pid = fork(0)) == 0){ if((pid = fork(0)) == 0){
m1 = 0; m1 = 0;
while((m2 = malloc(10001)) != 0){ while((m2 = malloc(10001)) != 0){
*(char**)m2 = m1; *(char**)m2 = (char*) m1;
m1 = m2; m1 = m2;
} }
while(m1){ while(m1){
...@@ -410,7 +412,7 @@ void ...@@ -410,7 +412,7 @@ void
twofiles(void) twofiles(void)
{ {
int fd, pid, i, j, n, total; int fd, pid, i, j, n, total;
char *fname; const char *fname;
printf(1, "twofiles test\n"); printf(1, "twofiles test\n");
...@@ -1450,7 +1452,7 @@ bigargtest(void) ...@@ -1450,7 +1452,7 @@ bigargtest(void)
pid = fork(0); pid = fork(0);
if(pid == 0){ if(pid == 0){
char *args[32+1]; const char *args[32+1];
int i; int i;
for(i = 0; i < 32; i++) for(i = 0; i < 32; i++)
args[i] = "bigargs test: failed\n "; args[i] = "bigargs test: failed\n ";
...@@ -1467,7 +1469,7 @@ bigargtest(void) ...@@ -1467,7 +1469,7 @@ bigargtest(void)
} }
void void
uox(char *name, char *data) uox(char *name, const char *data)
{ {
int fd = open(name, O_CREATE|O_RDWR); int fd = open(name, O_CREATE|O_RDWR);
if(fd < 0){ if(fd < 0){
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论