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

avoid kernel stack sharing by spawning distinct threads per core

上级 3a21ba17
...@@ -4,8 +4,11 @@ ...@@ -4,8 +4,11 @@
#include "user.h" #include "user.h"
#include "fcntl.h" #include "fcntl.h"
#include "mtrace.h" #include "mtrace.h"
#include "pthread.h"
static int cpu; static int cpu;
static pthread_barrier_t bar;
enum { ncore = 8 };
void void
next() next()
...@@ -18,60 +21,69 @@ next() ...@@ -18,60 +21,69 @@ next()
cpu++; cpu++;
} }
void void*
vmsharing(void) vmsharing(void* arg)
{ {
for (int i = 0; i < 8; i++) { u64 i = (u64) arg;
next();
volatile char *p = (char*)(0x40000UL + i * 4096); volatile char *p = (char*)(0x40000UL + i * 4096);
mtenable("xv6-mapsharing"); mtenable("xv6-mapsharing");
if (map((void *) p, 4096) < 0) if (map((void *) p, 4096) < 0)
die("map failed"); die("map failed");
mtdisable("xv6-mapsharing"); mtdisable("xv6-mapsharing");
mtenable("xv6-mapsharing"); mtenable("xv6-mapsharing");
if (unmap((void *) p, 4096) < 0) if (unmap((void *) p, 4096) < 0)
die("unmap failed"); die("unmap failed");
mtdisable("xv6-mapsharing"); mtdisable("xv6-mapsharing");
}
return 0;
} }
void void*
fssharing(void) fssharing(void* arg)
{ {
u64 i = (u64) arg;
// Note that we keep these files open; otherwise all of these // Note that we keep these files open; otherwise all of these
// operations will share the abstract FD object and we won't get any // operations will share the abstract FD object and we won't get any
// results. // results.
next(); char filename[32];
mtenable("xv6-fssharing"); snprintf(filename, sizeof(filename), "f%d", i);
open("a", O_CREATE|O_RDWR);
mtdisable("xv6-fssharing");
next();
mtenable("xv6-fssharing"); mtenable("xv6-fssharing");
open("b", O_CREATE|O_RDWR); open(filename, O_CREATE|O_RDWR);
mtdisable("xv6-fssharing"); mtdisable("xv6-fssharing");
next(); pthread_barrier_wait(&bar);
mtenable("xv6-fssharing");
open("a", O_RDWR);
mtdisable("xv6-fssharing");
next(); for (u64 j = 0; j < ncore; j++) {
mtenable("xv6-fssharing"); snprintf(filename, sizeof(filename), "f%d", j);
open("b", O_RDWR); mtenable("xv6-fssharing");
mtdisable("xv6-fssharing"); open(filename, O_RDWR);
mtdisable("xv6-fssharing");
}
return 0;
} }
int int
main(int ac, char **av) main(int ac, char **av)
{ {
void* (*op)(void*) = 0;
if (ac == 2 && strcmp(av[1], "vm") == 0) if (ac == 2 && strcmp(av[1], "vm") == 0)
vmsharing(); op = vmsharing;
else if (ac == 2 && strcmp(av[1], "fs") == 0) else if (ac == 2 && strcmp(av[1], "fs") == 0)
fssharing(); op = fssharing;
else else
fprintf(1, "usage: %s vm|fs\n", av[0]); fprintf(1, "usage: %s vm|fs\n", av[0]);
if (op) {
pthread_barrier_init(&bar, 0, ncore);
for (u64 i = 0; i < ncore; i++) {
next();
pthread_t tid;
pthread_create(&tid, 0, op, (void*) i);
}
}
} }
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论