2019-06-11 13:57:14 +00:00
|
|
|
#include "kernel/param.h"
|
|
|
|
#include "kernel/types.h"
|
|
|
|
#include "kernel/stat.h"
|
|
|
|
#include "user/user.h"
|
|
|
|
#include "kernel/fs.h"
|
|
|
|
#include "kernel/fcntl.h"
|
|
|
|
#include "kernel/syscall.h"
|
|
|
|
#include "kernel/memlayout.h"
|
2019-07-22 22:08:52 +00:00
|
|
|
#include "kernel/riscv.h"
|
2006-07-16 15:36:31 +00:00
|
|
|
|
2019-09-19 10:58:28 +00:00
|
|
|
//
|
|
|
|
// Tests xv6 system calls. usertests without arguments runs them all
|
|
|
|
// and usertests <name> runs <name> test. The test runner creates for
|
|
|
|
// each test a process and based on the exit status of the process,
|
|
|
|
// the test runner reports "OK" or "FAILED". Some tests result in
|
|
|
|
// kernel printing usertrap messages, which can be ignored if test
|
|
|
|
// prints "OK".
|
|
|
|
//
|
|
|
|
|
2020-10-04 12:44:32 +00:00
|
|
|
#define BUFSZ ((MAXOPBLOCKS+2)*BSIZE)
|
2019-08-20 16:58:00 +00:00
|
|
|
|
|
|
|
char buf[BUFSZ];
|
2006-09-07 13:23:41 +00:00
|
|
|
|
2022-08-25 13:45:35 +00:00
|
|
|
//
|
|
|
|
// Section with tests that run fairly quickly. Use -q if you want to
|
|
|
|
// run just those. With -q usertests also runs the ones that take a
|
|
|
|
// fair of time.
|
|
|
|
//
|
|
|
|
|
2020-08-07 20:56:00 +00:00
|
|
|
// what if you pass ridiculous pointers to system calls
|
|
|
|
// that read user memory with copyin?
|
2020-08-07 18:34:39 +00:00
|
|
|
void
|
2020-08-07 19:06:43 +00:00
|
|
|
copyin(char *s)
|
2020-08-07 18:34:39 +00:00
|
|
|
{
|
2020-08-07 19:06:43 +00:00
|
|
|
uint64 addrs[] = { 0x80000000LL, 0xffffffffffffffff };
|
2020-08-07 18:34:39 +00:00
|
|
|
|
2020-08-07 19:06:43 +00:00
|
|
|
for(int ai = 0; ai < 2; ai++){
|
|
|
|
uint64 addr = addrs[ai];
|
|
|
|
|
|
|
|
int fd = open("copyin1", O_CREATE|O_WRONLY);
|
|
|
|
if(fd < 0){
|
|
|
|
printf("open(copyin1) failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
int n = write(fd, (void*)addr, 8192);
|
|
|
|
if(n >= 0){
|
|
|
|
printf("write(fd, %p, 8192) returned %d, not -1\n", addr, n);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
unlink("copyin1");
|
|
|
|
|
|
|
|
n = write(1, (char*)addr, 8192);
|
|
|
|
if(n > 0){
|
|
|
|
printf("write(1, %p, 8192) returned %d, not -1 or 0\n", addr, n);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int fds[2];
|
|
|
|
if(pipe(fds) < 0){
|
|
|
|
printf("pipe() failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
n = write(fds[1], (char*)addr, 8192);
|
|
|
|
if(n > 0){
|
|
|
|
printf("write(pipe, %p, 8192) returned %d, not -1 or 0\n", addr, n);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(fds[0]);
|
|
|
|
close(fds[1]);
|
2020-08-07 18:34:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 20:56:00 +00:00
|
|
|
// what if you pass ridiculous pointers to system calls
|
|
|
|
// that write user memory with copyout?
|
2020-08-07 18:34:39 +00:00
|
|
|
void
|
2020-08-07 19:06:43 +00:00
|
|
|
copyout(char *s)
|
2020-08-07 18:34:39 +00:00
|
|
|
{
|
2020-08-07 19:06:43 +00:00
|
|
|
uint64 addrs[] = { 0x80000000LL, 0xffffffffffffffff };
|
2020-08-07 18:34:39 +00:00
|
|
|
|
2020-08-07 19:06:43 +00:00
|
|
|
for(int ai = 0; ai < 2; ai++){
|
|
|
|
uint64 addr = addrs[ai];
|
2020-08-07 18:34:39 +00:00
|
|
|
|
2020-08-07 19:06:43 +00:00
|
|
|
int fd = open("README", 0);
|
|
|
|
if(fd < 0){
|
|
|
|
printf("open(README) failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
int n = read(fd, (void*)addr, 8192);
|
|
|
|
if(n > 0){
|
|
|
|
printf("read(fd, %p, 8192) returned %d, not -1 or 0\n", addr, n);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
int fds[2];
|
|
|
|
if(pipe(fds) < 0){
|
|
|
|
printf("pipe() failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
n = write(fds[1], "x", 1);
|
|
|
|
if(n != 1){
|
|
|
|
printf("pipe write failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
n = read(fds[0], (void*)addr, 8192);
|
|
|
|
if(n > 0){
|
|
|
|
printf("read(pipe, %p, 8192) returned %d, not -1 or 0\n", addr, n);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(fds[0]);
|
|
|
|
close(fds[1]);
|
2020-08-07 18:34:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 20:56:00 +00:00
|
|
|
// what if you pass ridiculous string pointers to system calls?
|
2020-08-07 18:34:39 +00:00
|
|
|
void
|
2020-08-07 20:39:56 +00:00
|
|
|
copyinstr1(char *s)
|
2020-08-07 18:34:39 +00:00
|
|
|
{
|
2020-08-07 19:06:43 +00:00
|
|
|
uint64 addrs[] = { 0x80000000LL, 0xffffffffffffffff };
|
|
|
|
|
|
|
|
for(int ai = 0; ai < 2; ai++){
|
|
|
|
uint64 addr = addrs[ai];
|
|
|
|
|
|
|
|
int fd = open((char *)addr, O_CREATE|O_WRONLY);
|
|
|
|
if(fd >= 0){
|
|
|
|
printf("open(%p) returned %d, not -1\n", addr, fd);
|
|
|
|
exit(1);
|
|
|
|
}
|
2020-08-07 18:34:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 20:56:00 +00:00
|
|
|
// what if a string system call argument is exactly the size
|
|
|
|
// of the kernel buffer it is copied into, so that the null
|
|
|
|
// would fall just beyond the end of the kernel buffer?
|
2020-08-07 20:39:56 +00:00
|
|
|
void
|
|
|
|
copyinstr2(char *s)
|
|
|
|
{
|
|
|
|
char b[MAXPATH+1];
|
|
|
|
|
|
|
|
for(int i = 0; i < MAXPATH; i++)
|
|
|
|
b[i] = 'x';
|
|
|
|
b[MAXPATH] = '\0';
|
|
|
|
|
|
|
|
int ret = unlink(b);
|
|
|
|
if(ret != -1){
|
|
|
|
printf("unlink(%s) returned %d, not -1\n", b, ret);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = open(b, O_CREATE | O_WRONLY);
|
|
|
|
if(fd != -1){
|
|
|
|
printf("open(%s) returned %d, not -1\n", b, fd);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = link(b, b);
|
|
|
|
if(ret != -1){
|
|
|
|
printf("link(%s, %s) returned %d, not -1\n", b, b, ret);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *args[] = { "xx", 0 };
|
|
|
|
ret = exec(b, args);
|
|
|
|
if(ret != -1){
|
|
|
|
printf("exec(%s) returned %d, not -1\n", b, fd);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pid = fork();
|
|
|
|
if(pid < 0){
|
|
|
|
printf("fork failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(pid == 0){
|
|
|
|
static char big[PGSIZE+1];
|
|
|
|
for(int i = 0; i < PGSIZE; i++)
|
|
|
|
big[i] = 'x';
|
|
|
|
big[PGSIZE] = '\0';
|
|
|
|
char *args2[] = { big, big, big, 0 };
|
|
|
|
ret = exec("echo", args2);
|
|
|
|
if(ret != -1){
|
|
|
|
printf("exec(echo, BIG) returned %d, not -1\n", fd);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
exit(747); // OK
|
|
|
|
}
|
|
|
|
|
|
|
|
int st = 0;
|
|
|
|
wait(&st);
|
|
|
|
if(st != 747){
|
|
|
|
printf("exec(echo, BIG) succeeded, should have failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 20:56:00 +00:00
|
|
|
// what if a string argument crosses over the end of last user page?
|
|
|
|
void
|
|
|
|
copyinstr3(char *s)
|
|
|
|
{
|
|
|
|
sbrk(8192);
|
|
|
|
uint64 top = (uint64) sbrk(0);
|
|
|
|
if((top % PGSIZE) != 0){
|
|
|
|
sbrk(PGSIZE - (top % PGSIZE));
|
|
|
|
}
|
|
|
|
top = (uint64) sbrk(0);
|
|
|
|
if(top % PGSIZE){
|
|
|
|
printf("oops\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *b = (char *) (top - 1);
|
|
|
|
*b = 'x';
|
|
|
|
|
|
|
|
int ret = unlink(b);
|
|
|
|
if(ret != -1){
|
|
|
|
printf("unlink(%s) returned %d, not -1\n", b, ret);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = open(b, O_CREATE | O_WRONLY);
|
|
|
|
if(fd != -1){
|
|
|
|
printf("open(%s) returned %d, not -1\n", b, fd);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = link(b, b);
|
|
|
|
if(ret != -1){
|
|
|
|
printf("link(%s, %s) returned %d, not -1\n", b, b, ret);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *args[] = { "xx", 0 };
|
|
|
|
ret = exec(b, args);
|
|
|
|
if(ret != -1){
|
|
|
|
printf("exec(%s) returned %d, not -1\n", b, fd);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 17:18:54 +00:00
|
|
|
// See if the kernel refuses to read/write user memory that the
|
|
|
|
// application doesn't have anymore, because it returned it.
|
|
|
|
void
|
|
|
|
rwsbrk()
|
|
|
|
{
|
|
|
|
int fd, n;
|
|
|
|
|
|
|
|
uint64 a = (uint64) sbrk(8192);
|
|
|
|
|
|
|
|
if(a == 0xffffffffffffffffLL) {
|
|
|
|
printf("sbrk(rwsbrk) failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((uint64) sbrk(-8192) == 0xffffffffffffffffLL) {
|
|
|
|
printf("sbrk(rwsbrk) shrink failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open("rwsbrk", O_CREATE|O_WRONLY);
|
|
|
|
if(fd < 0){
|
|
|
|
printf("open(rwsbrk) failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
n = write(fd, (void*)(a+4096), 1024);
|
|
|
|
if(n >= 0){
|
|
|
|
printf("write(fd, %p, 1024) returned %d, not -1\n", a+4096, n);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
unlink("rwsbrk");
|
|
|
|
|
|
|
|
fd = open("README", O_RDONLY);
|
|
|
|
if(fd < 0){
|
|
|
|
printf("open(rwsbrk) failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
n = read(fd, (void*)(a+4096), 10);
|
|
|
|
if(n >= 0){
|
|
|
|
printf("read(fd, %p, 10) returned %d, not -1\n", a+4096, n);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:38:08 +00:00
|
|
|
// test O_TRUNC.
|
|
|
|
void
|
|
|
|
truncate1(char *s)
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
|
|
|
|
unlink("truncfile");
|
|
|
|
int fd1 = open("truncfile", O_CREATE|O_WRONLY|O_TRUNC);
|
|
|
|
write(fd1, "abcd", 4);
|
|
|
|
close(fd1);
|
|
|
|
|
|
|
|
int fd2 = open("truncfile", O_RDONLY);
|
|
|
|
int n = read(fd2, buf, sizeof(buf));
|
|
|
|
if(n != 4){
|
|
|
|
printf("%s: read %d bytes, wanted 4\n", s, n);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fd1 = open("truncfile", O_WRONLY|O_TRUNC);
|
|
|
|
|
|
|
|
int fd3 = open("truncfile", O_RDONLY);
|
|
|
|
n = read(fd3, buf, sizeof(buf));
|
|
|
|
if(n != 0){
|
|
|
|
printf("aaa fd3=%d\n", fd3);
|
|
|
|
printf("%s: read %d bytes, wanted 0\n", s, n);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
n = read(fd2, buf, sizeof(buf));
|
|
|
|
if(n != 0){
|
|
|
|
printf("bbb fd2=%d\n", fd2);
|
|
|
|
printf("%s: read %d bytes, wanted 0\n", s, n);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
write(fd1, "abcdef", 6);
|
|
|
|
|
|
|
|
n = read(fd3, buf, sizeof(buf));
|
|
|
|
if(n != 6){
|
|
|
|
printf("%s: read %d bytes, wanted 6\n", s, n);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
n = read(fd2, buf, sizeof(buf));
|
|
|
|
if(n != 2){
|
|
|
|
printf("%s: read %d bytes, wanted 2\n", s, n);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink("truncfile");
|
|
|
|
|
|
|
|
close(fd1);
|
|
|
|
close(fd2);
|
|
|
|
close(fd3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write to an open FD whose file has just been truncated.
|
|
|
|
// this causes a write at an offset beyond the end of the file.
|
|
|
|
// such writes fail on xv6 (unlike POSIX) but at least
|
|
|
|
// they don't crash.
|
|
|
|
void
|
|
|
|
truncate2(char *s)
|
|
|
|
{
|
|
|
|
unlink("truncfile");
|
|
|
|
|
|
|
|
int fd1 = open("truncfile", O_CREATE|O_TRUNC|O_WRONLY);
|
|
|
|
write(fd1, "abcd", 4);
|
|
|
|
|
|
|
|
int fd2 = open("truncfile", O_TRUNC|O_WRONLY);
|
|
|
|
|
|
|
|
int n = write(fd1, "x", 1);
|
|
|
|
if(n != -1){
|
|
|
|
printf("%s: write returned %d, expected -1\n", s, n);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink("truncfile");
|
|
|
|
close(fd1);
|
|
|
|
close(fd2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
truncate3(char *s)
|
|
|
|
{
|
|
|
|
int pid, xstatus;
|
|
|
|
|
|
|
|
close(open("truncfile", O_CREATE|O_TRUNC|O_WRONLY));
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if(pid < 0){
|
|
|
|
printf("%s: fork failed\n", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pid == 0){
|
|
|
|
for(int i = 0; i < 100; i++){
|
|
|
|
char buf[32];
|
|
|
|
int fd = open("truncfile", O_WRONLY);
|
|
|
|
if(fd < 0){
|
|
|
|
printf("%s: open failed\n", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
int n = write(fd, "1234567890", 10);
|
|
|
|
if(n != 10){
|
|
|
|
printf("%s: write got %d, expected 10\n", s, n);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
fd = open("truncfile", O_RDONLY);
|
|
|
|
read(fd, buf, sizeof(buf));
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < 150; i++){
|
|
|
|
int fd = open("truncfile", O_CREATE|O_WRONLY|O_TRUNC);
|
|
|
|
if(fd < 0){
|
|
|
|
printf("%s: open failed\n", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
int n = write(fd, "xxx", 3);
|
|
|
|
if(n != 3){
|
|
|
|
printf("%s: write got %d, expected 3\n", s, n);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
wait(&xstatus);
|
|
|
|
unlink("truncfile");
|
|
|
|
exit(xstatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-04 17:06:48 +00:00
|
|
|
// does chdir() call iput(p->cwd) in a transaction?
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
iputtest(char *s)
|
2014-08-04 17:06:48 +00:00
|
|
|
{
|
|
|
|
if(mkdir("iputdir") < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: mkdir failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
|
|
|
if(chdir("iputdir") < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: chdir iputdir failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
|
|
|
if(unlink("../iputdir") < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink ../iputdir failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
|
|
|
if(chdir("/") < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: chdir / failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// does exit() call iput(p->cwd) in a transaction?
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
exitiputtest(char *s)
|
2014-08-04 17:06:48 +00:00
|
|
|
{
|
2019-09-19 10:58:28 +00:00
|
|
|
int pid, xstatus;
|
2014-08-04 17:06:48 +00:00
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if(pid < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: fork failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
|
|
|
if(pid == 0){
|
|
|
|
if(mkdir("iputdir") < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: mkdir failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
|
|
|
if(chdir("iputdir") < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: child chdir failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
|
|
|
if(unlink("../iputdir") < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink ../iputdir failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
2019-09-19 10:58:28 +00:00
|
|
|
wait(&xstatus);
|
|
|
|
exit(xstatus);
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// does the error path in open() for attempt to write a
|
|
|
|
// directory call iput() in a transaction?
|
|
|
|
// needs a hacked kernel that pauses just after the namei()
|
|
|
|
// call in sys_open():
|
|
|
|
// if((ip = namei(path)) == 0)
|
|
|
|
// return -1;
|
|
|
|
// {
|
|
|
|
// int i;
|
|
|
|
// for(i = 0; i < 10000; i++)
|
|
|
|
// yield();
|
|
|
|
// }
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
openiputtest(char *s)
|
2014-08-04 17:06:48 +00:00
|
|
|
{
|
2019-09-19 10:58:28 +00:00
|
|
|
int pid, xstatus;
|
2014-08-04 17:06:48 +00:00
|
|
|
|
|
|
|
if(mkdir("oidir") < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: mkdir oidir failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
|
|
|
pid = fork();
|
|
|
|
if(pid < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: fork failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
|
|
|
if(pid == 0){
|
|
|
|
int fd = open("oidir", O_RDWR);
|
|
|
|
if(fd >= 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: open directory for write succeeded\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
|
|
|
sleep(1);
|
|
|
|
if(unlink("oidir") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
2019-09-19 10:58:28 +00:00
|
|
|
wait(&xstatus);
|
|
|
|
exit(xstatus);
|
2014-08-04 17:06:48 +00:00
|
|
|
}
|
|
|
|
|
2006-09-07 13:23:41 +00:00
|
|
|
// simple file system tests
|
|
|
|
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
opentest(char *s)
|
2006-09-07 13:23:41 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open("echo", 0);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: open echo failed!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
fd = open("doesnotexist", 0);
|
|
|
|
if(fd >= 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: open doesnotexist succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
writetest(char *s)
|
2006-09-07 13:23:41 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int i;
|
2019-08-20 16:58:00 +00:00
|
|
|
enum { N=100, SZ=10 };
|
|
|
|
|
2006-09-07 13:23:41 +00:00
|
|
|
fd = open("small", O_CREATE|O_RDWR);
|
2019-09-19 10:58:28 +00:00
|
|
|
if(fd < 0){
|
|
|
|
printf("%s: error: creat small failed!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2019-08-20 16:58:00 +00:00
|
|
|
for(i = 0; i < N; i++){
|
|
|
|
if(write(fd, "aaaaaaaaaa", SZ) != SZ){
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("%s: error: write aa %d new file failed\n", s, i);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2019-08-20 16:58:00 +00:00
|
|
|
if(write(fd, "bbbbbbbbbb", SZ) != SZ){
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("%s: error: write bb %d new file failed\n", s, i);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
fd = open("small", O_RDONLY);
|
2019-09-19 10:58:28 +00:00
|
|
|
if(fd < 0){
|
|
|
|
printf("%s: error: open small failed!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2019-08-20 16:58:00 +00:00
|
|
|
i = read(fd, buf, N*SZ*2);
|
2019-09-19 10:58:28 +00:00
|
|
|
if(i != N*SZ*2){
|
|
|
|
printf("%s: read failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
2011-01-11 18:01:13 +00:00
|
|
|
if(unlink("small") < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink small failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
writebig(char *s)
|
2006-09-07 13:23:41 +00:00
|
|
|
{
|
|
|
|
int i, fd, n;
|
|
|
|
|
|
|
|
fd = open("big", O_CREATE|O_RDWR);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: error: creat big failed!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
2011-01-11 18:01:13 +00:00
|
|
|
for(i = 0; i < MAXFILE; i++){
|
|
|
|
((int*)buf)[0] = i;
|
2019-08-20 16:58:00 +00:00
|
|
|
if(write(fd, buf, BSIZE) != BSIZE){
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("%s: error: write big file failed\n", s, i);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
fd = open("big", O_RDONLY);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: error: open big failed!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n = 0;
|
2011-01-11 18:01:13 +00:00
|
|
|
for(;;){
|
2019-08-20 16:58:00 +00:00
|
|
|
i = read(fd, buf, BSIZE);
|
2011-01-11 18:01:13 +00:00
|
|
|
if(i == 0){
|
|
|
|
if(n == MAXFILE - 1){
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("%s: read only %d blocks from big", s, n);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
break;
|
2019-08-20 16:58:00 +00:00
|
|
|
} else if(i != BSIZE){
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("%s: read failed %d\n", s, i);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2011-01-11 18:01:13 +00:00
|
|
|
if(((int*)buf)[0] != n){
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("%s: read content of block %d is %d\n", s,
|
2006-09-07 13:23:41 +00:00
|
|
|
n, ((int*)buf)[0]);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
close(fd);
|
2011-01-11 18:01:13 +00:00
|
|
|
if(unlink("big") < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink big failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 10:58:28 +00:00
|
|
|
// many creates, followed by unlink test
|
2006-09-07 13:23:41 +00:00
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
createtest(char *s)
|
2006-09-07 13:23:41 +00:00
|
|
|
{
|
|
|
|
int i, fd;
|
2019-08-20 16:58:00 +00:00
|
|
|
enum { N=52 };
|
2006-09-07 13:23:41 +00:00
|
|
|
|
2020-10-04 12:44:32 +00:00
|
|
|
char name[3];
|
2006-09-07 13:23:41 +00:00
|
|
|
name[0] = 'a';
|
|
|
|
name[2] = '\0';
|
2019-08-20 16:58:00 +00:00
|
|
|
for(i = 0; i < N; i++){
|
2006-09-07 13:23:41 +00:00
|
|
|
name[1] = '0' + i;
|
|
|
|
fd = open(name, O_CREATE|O_RDWR);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
name[0] = 'a';
|
|
|
|
name[2] = '\0';
|
2019-08-20 16:58:00 +00:00
|
|
|
for(i = 0; i < N; i++){
|
2006-09-07 13:23:41 +00:00
|
|
|
name[1] = '0' + i;
|
|
|
|
unlink(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 10:58:28 +00:00
|
|
|
void dirtest(char *s)
|
2006-09-07 13:23:41 +00:00
|
|
|
{
|
2011-01-11 18:01:13 +00:00
|
|
|
if(mkdir("dir0") < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: mkdir failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
2011-01-11 18:01:13 +00:00
|
|
|
if(chdir("dir0") < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: chdir dir0 failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
2011-01-11 18:01:13 +00:00
|
|
|
if(chdir("..") < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: chdir .. failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
2011-01-11 18:01:13 +00:00
|
|
|
if(unlink("dir0") < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink dir0 failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
exectest(char *s)
|
2006-09-07 13:23:41 +00:00
|
|
|
{
|
2019-09-19 19:14:52 +00:00
|
|
|
int fd, xstatus, pid;
|
|
|
|
char *echoargv[] = { "echo", "OK", 0 };
|
|
|
|
char buf[3];
|
|
|
|
|
|
|
|
unlink("echo-ok");
|
|
|
|
pid = fork();
|
|
|
|
if(pid < 0) {
|
|
|
|
printf("%s: fork failed\n", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(pid == 0) {
|
|
|
|
close(1);
|
|
|
|
fd = open("echo-ok", O_CREATE|O_WRONLY);
|
|
|
|
if(fd < 0) {
|
|
|
|
printf("%s: create failed\n", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(fd != 1) {
|
|
|
|
printf("%s: wrong fd\n", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(exec("echo", echoargv) < 0){
|
|
|
|
printf("%s: exec echo failed\n", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
// won't get to here
|
|
|
|
}
|
|
|
|
if (wait(&xstatus) != pid) {
|
|
|
|
printf("%s: wait failed!\n", s);
|
|
|
|
}
|
|
|
|
if(xstatus != 0)
|
|
|
|
exit(xstatus);
|
|
|
|
|
|
|
|
fd = open("echo-ok", O_RDONLY);
|
|
|
|
if(fd < 0) {
|
|
|
|
printf("%s: open failed\n", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (read(fd, buf, 2) != 2) {
|
|
|
|
printf("%s: read failed\n", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
unlink("echo-ok");
|
|
|
|
if(buf[0] == 'O' && buf[1] == 'K')
|
|
|
|
exit(0);
|
|
|
|
else {
|
|
|
|
printf("%s: wrong output\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2019-09-19 19:14:52 +00:00
|
|
|
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2006-06-27 14:35:53 +00:00
|
|
|
|
2006-07-11 17:39:45 +00:00
|
|
|
// simple fork and pipe read/write
|
|
|
|
|
2006-06-27 14:35:53 +00:00
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
pipe1(char *s)
|
2006-06-27 14:35:53 +00:00
|
|
|
{
|
2019-09-19 10:58:28 +00:00
|
|
|
int fds[2], pid, xstatus;
|
2007-08-10 17:17:42 +00:00
|
|
|
int seq, i, n, cc, total;
|
2019-08-20 16:58:00 +00:00
|
|
|
enum { N=5, SZ=1033 };
|
|
|
|
|
2006-08-29 19:06:37 +00:00
|
|
|
if(pipe(fds) != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: pipe() failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-08-29 19:06:37 +00:00
|
|
|
}
|
2006-07-01 21:26:01 +00:00
|
|
|
pid = fork();
|
2007-08-10 17:17:42 +00:00
|
|
|
seq = 0;
|
2006-06-27 14:35:53 +00:00
|
|
|
if(pid == 0){
|
2006-07-01 21:26:01 +00:00
|
|
|
close(fds[0]);
|
2019-08-20 16:58:00 +00:00
|
|
|
for(n = 0; n < N; n++){
|
|
|
|
for(i = 0; i < SZ; i++)
|
2006-07-01 21:26:01 +00:00
|
|
|
buf[i] = seq++;
|
2019-08-20 16:58:00 +00:00
|
|
|
if(write(fds[1], buf, SZ) != SZ){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: pipe1 oops 1\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-07-01 21:26:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2006-08-29 19:06:37 +00:00
|
|
|
} else if(pid > 0){
|
2006-07-01 21:26:01 +00:00
|
|
|
close(fds[1]);
|
|
|
|
total = 0;
|
|
|
|
cc = 1;
|
2006-07-17 01:25:22 +00:00
|
|
|
while((n = read(fds[0], buf, cc)) > 0){
|
2006-07-01 21:26:01 +00:00
|
|
|
for(i = 0; i < n; i++){
|
|
|
|
if((buf[i] & 0xff) != (seq++ & 0xff)){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: pipe1 oops 2\n", s);
|
2006-07-01 21:26:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
total += n;
|
|
|
|
cc = cc * 2;
|
|
|
|
if(cc > sizeof(buf))
|
|
|
|
cc = sizeof(buf);
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|
2019-08-20 16:58:00 +00:00
|
|
|
if(total != N * SZ){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: pipe1 oops 3 total %d\n", total);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2011-08-31 00:50:19 +00:00
|
|
|
}
|
2006-07-01 21:26:01 +00:00
|
|
|
close(fds[0]);
|
2019-09-19 10:58:28 +00:00
|
|
|
wait(&xstatus);
|
|
|
|
exit(xstatus);
|
2006-08-29 19:06:37 +00:00
|
|
|
} else {
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: fork() failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 22:32:35 +00:00
|
|
|
|
|
|
|
// test if child is killed (status = -1)
|
|
|
|
void
|
|
|
|
killstatus(char *s)
|
|
|
|
{
|
|
|
|
int xst;
|
|
|
|
|
|
|
|
for(int i = 0; i < 100; i++){
|
|
|
|
int pid1 = fork();
|
|
|
|
if(pid1 < 0){
|
|
|
|
printf("%s: fork failed\n", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(pid1 == 0){
|
2020-11-06 00:00:37 +00:00
|
|
|
while(1) {
|
2020-11-05 22:32:35 +00:00
|
|
|
getpid();
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
sleep(1);
|
|
|
|
kill(pid1);
|
|
|
|
wait(&xst);
|
|
|
|
if(xst != -1) {
|
|
|
|
printf("%s: status should be -1\n", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2006-07-11 17:39:45 +00:00
|
|
|
// meant to be run w/ at most two CPUs
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
preempt(char *s)
|
2006-07-11 17:39:45 +00:00
|
|
|
{
|
|
|
|
int pid1, pid2, pid3;
|
|
|
|
int pfds[2];
|
|
|
|
|
|
|
|
pid1 = fork();
|
Checkpoint port of xv6 to x86-64. Passed usertests on 2 processors a few times.
The x86-64 doesn't just add two levels to page tables to support 64 bit
addresses, but is a different processor. For example, calling conventions,
system calls, and segmentation are different from 32-bit x86. Segmentation is
basically gone, but gs/fs in combination with MSRs can be used to hold a
per-core pointer. In general, x86-64 is more straightforward than 32-bit
x86. The port uses code from sv6 and the xv6 "rsc-amd64" branch.
A summary of the changes is as follows:
- Booting: switch to grub instead of xv6's bootloader (pass -kernel to qemu),
because xv6's boot loader doesn't understand 64bit ELF files. And, we don't
care anymore about booting.
- Makefile: use -m64 instead of -m32 flag for gcc, delete boot loader, xv6.img,
bochs, and memfs. For now dont' use -O2, since usertests with -O2 is bigger than
MAXFILE!
- Update gdb.tmpl to be for i386 or x86-64
- Console/printf: use stdarg.h and treat 64-bit addresses different from ints
(32-bit)
- Update elfhdr to be 64 bit
- entry.S/entryother.S: add code to switch to 64-bit mode: build a simple page
table in 32-bit mode before switching to 64-bit mode, share code for entering
boot processor and APs, and tweak boot gdt. The boot gdt is the gdt that the
kernel proper also uses. (In 64-bit mode, the gdt/segmentation and task state
mostly disappear.)
- exec.c: fix passing argv (64-bit now instead of 32-bit).
- initcode.c: use syscall instead of int.
- kernel.ld: load kernel very high, in top terabyte. 64 bits is a lot of
address space!
- proc.c: initial return is through new syscall path instead of trapret.
- proc.h: update struct cpu to have some scratch space since syscall saves less
state than int, update struct context to reflect x86-64 calling conventions.
- swtch: simplify for x86-64 calling conventions.
- syscall: add fetcharg to handle x86-64 calling convetions (6 arguments are
passed through registers), and fetchaddr to read a 64-bit value from user space.
- sysfile: update to handle pointers from user space (e.g., sys_exec), which are
64 bits.
- trap.c: no special trap vector for sys calls, because x86-64 has a different
plan for system calls.
- trapasm: one plan for syscalls and one plan for traps (interrupt and
exceptions). On x86-64, the kernel is responsible for switching user/kernel
stacks. To do, xv6 keeps some scratch space in the cpu structure, and uses MSR
GS_KERN_BASE to point to the core's cpu structure (using swapgs).
- types.h: add uint64, and change pde_t to uint64
- usertests: exit() when fork fails, which helped in tracking down one of the
bugs in the switch from 32-bit to 64-bit
- vectors: update to make them 64 bits
- vm.c: use bootgdt in kernel too, program MSRs for syscalls and core-local
state (for swapgs), walk 4 levels in walkpgdir, add DEVSPACETOP, use task
segment to set kernel stack for interrupts (but simpler than in 32-bit mode),
add an extra argument to freevm (size of user part of address space) to avoid
checking all entries till KERNBASE (there are MANY TB before the top 1TB).
- x86: update trapframe to have 64-bit entries, which is what the processor
pushes on syscalls and traps. simplify lgdt and lidt, using struct desctr,
which needs the gcc directives packed and aligned.
TODO:
- use int32 instead of int?
- simplify curproc(). xv6 has per-cpu state again, but this time it must have it.
- avoid repetition in walkpgdir
- fix validateint() in usertests.c
- fix bugs (e.g., observed one a case of entering kernel with invalid gs or proc
2018-09-23 12:24:42 +00:00
|
|
|
if(pid1 < 0) {
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("%s: fork failed", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
Checkpoint port of xv6 to x86-64. Passed usertests on 2 processors a few times.
The x86-64 doesn't just add two levels to page tables to support 64 bit
addresses, but is a different processor. For example, calling conventions,
system calls, and segmentation are different from 32-bit x86. Segmentation is
basically gone, but gs/fs in combination with MSRs can be used to hold a
per-core pointer. In general, x86-64 is more straightforward than 32-bit
x86. The port uses code from sv6 and the xv6 "rsc-amd64" branch.
A summary of the changes is as follows:
- Booting: switch to grub instead of xv6's bootloader (pass -kernel to qemu),
because xv6's boot loader doesn't understand 64bit ELF files. And, we don't
care anymore about booting.
- Makefile: use -m64 instead of -m32 flag for gcc, delete boot loader, xv6.img,
bochs, and memfs. For now dont' use -O2, since usertests with -O2 is bigger than
MAXFILE!
- Update gdb.tmpl to be for i386 or x86-64
- Console/printf: use stdarg.h and treat 64-bit addresses different from ints
(32-bit)
- Update elfhdr to be 64 bit
- entry.S/entryother.S: add code to switch to 64-bit mode: build a simple page
table in 32-bit mode before switching to 64-bit mode, share code for entering
boot processor and APs, and tweak boot gdt. The boot gdt is the gdt that the
kernel proper also uses. (In 64-bit mode, the gdt/segmentation and task state
mostly disappear.)
- exec.c: fix passing argv (64-bit now instead of 32-bit).
- initcode.c: use syscall instead of int.
- kernel.ld: load kernel very high, in top terabyte. 64 bits is a lot of
address space!
- proc.c: initial return is through new syscall path instead of trapret.
- proc.h: update struct cpu to have some scratch space since syscall saves less
state than int, update struct context to reflect x86-64 calling conventions.
- swtch: simplify for x86-64 calling conventions.
- syscall: add fetcharg to handle x86-64 calling convetions (6 arguments are
passed through registers), and fetchaddr to read a 64-bit value from user space.
- sysfile: update to handle pointers from user space (e.g., sys_exec), which are
64 bits.
- trap.c: no special trap vector for sys calls, because x86-64 has a different
plan for system calls.
- trapasm: one plan for syscalls and one plan for traps (interrupt and
exceptions). On x86-64, the kernel is responsible for switching user/kernel
stacks. To do, xv6 keeps some scratch space in the cpu structure, and uses MSR
GS_KERN_BASE to point to the core's cpu structure (using swapgs).
- types.h: add uint64, and change pde_t to uint64
- usertests: exit() when fork fails, which helped in tracking down one of the
bugs in the switch from 32-bit to 64-bit
- vectors: update to make them 64 bits
- vm.c: use bootgdt in kernel too, program MSRs for syscalls and core-local
state (for swapgs), walk 4 levels in walkpgdir, add DEVSPACETOP, use task
segment to set kernel stack for interrupts (but simpler than in 32-bit mode),
add an extra argument to freevm (size of user part of address space) to avoid
checking all entries till KERNBASE (there are MANY TB before the top 1TB).
- x86: update trapframe to have 64-bit entries, which is what the processor
pushes on syscalls and traps. simplify lgdt and lidt, using struct desctr,
which needs the gcc directives packed and aligned.
TODO:
- use int32 instead of int?
- simplify curproc(). xv6 has per-cpu state again, but this time it must have it.
- avoid repetition in walkpgdir
- fix validateint() in usertests.c
- fix bugs (e.g., observed one a case of entering kernel with invalid gs or proc
2018-09-23 12:24:42 +00:00
|
|
|
}
|
2006-07-11 17:39:45 +00:00
|
|
|
if(pid1 == 0)
|
2006-07-17 01:25:22 +00:00
|
|
|
for(;;)
|
2006-07-11 17:39:45 +00:00
|
|
|
;
|
2006-09-06 17:27:19 +00:00
|
|
|
|
2006-07-11 17:39:45 +00:00
|
|
|
pid2 = fork();
|
Checkpoint port of xv6 to x86-64. Passed usertests on 2 processors a few times.
The x86-64 doesn't just add two levels to page tables to support 64 bit
addresses, but is a different processor. For example, calling conventions,
system calls, and segmentation are different from 32-bit x86. Segmentation is
basically gone, but gs/fs in combination with MSRs can be used to hold a
per-core pointer. In general, x86-64 is more straightforward than 32-bit
x86. The port uses code from sv6 and the xv6 "rsc-amd64" branch.
A summary of the changes is as follows:
- Booting: switch to grub instead of xv6's bootloader (pass -kernel to qemu),
because xv6's boot loader doesn't understand 64bit ELF files. And, we don't
care anymore about booting.
- Makefile: use -m64 instead of -m32 flag for gcc, delete boot loader, xv6.img,
bochs, and memfs. For now dont' use -O2, since usertests with -O2 is bigger than
MAXFILE!
- Update gdb.tmpl to be for i386 or x86-64
- Console/printf: use stdarg.h and treat 64-bit addresses different from ints
(32-bit)
- Update elfhdr to be 64 bit
- entry.S/entryother.S: add code to switch to 64-bit mode: build a simple page
table in 32-bit mode before switching to 64-bit mode, share code for entering
boot processor and APs, and tweak boot gdt. The boot gdt is the gdt that the
kernel proper also uses. (In 64-bit mode, the gdt/segmentation and task state
mostly disappear.)
- exec.c: fix passing argv (64-bit now instead of 32-bit).
- initcode.c: use syscall instead of int.
- kernel.ld: load kernel very high, in top terabyte. 64 bits is a lot of
address space!
- proc.c: initial return is through new syscall path instead of trapret.
- proc.h: update struct cpu to have some scratch space since syscall saves less
state than int, update struct context to reflect x86-64 calling conventions.
- swtch: simplify for x86-64 calling conventions.
- syscall: add fetcharg to handle x86-64 calling convetions (6 arguments are
passed through registers), and fetchaddr to read a 64-bit value from user space.
- sysfile: update to handle pointers from user space (e.g., sys_exec), which are
64 bits.
- trap.c: no special trap vector for sys calls, because x86-64 has a different
plan for system calls.
- trapasm: one plan for syscalls and one plan for traps (interrupt and
exceptions). On x86-64, the kernel is responsible for switching user/kernel
stacks. To do, xv6 keeps some scratch space in the cpu structure, and uses MSR
GS_KERN_BASE to point to the core's cpu structure (using swapgs).
- types.h: add uint64, and change pde_t to uint64
- usertests: exit() when fork fails, which helped in tracking down one of the
bugs in the switch from 32-bit to 64-bit
- vectors: update to make them 64 bits
- vm.c: use bootgdt in kernel too, program MSRs for syscalls and core-local
state (for swapgs), walk 4 levels in walkpgdir, add DEVSPACETOP, use task
segment to set kernel stack for interrupts (but simpler than in 32-bit mode),
add an extra argument to freevm (size of user part of address space) to avoid
checking all entries till KERNBASE (there are MANY TB before the top 1TB).
- x86: update trapframe to have 64-bit entries, which is what the processor
pushes on syscalls and traps. simplify lgdt and lidt, using struct desctr,
which needs the gcc directives packed and aligned.
TODO:
- use int32 instead of int?
- simplify curproc(). xv6 has per-cpu state again, but this time it must have it.
- avoid repetition in walkpgdir
- fix validateint() in usertests.c
- fix bugs (e.g., observed one a case of entering kernel with invalid gs or proc
2018-09-23 12:24:42 +00:00
|
|
|
if(pid2 < 0) {
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: fork failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
Checkpoint port of xv6 to x86-64. Passed usertests on 2 processors a few times.
The x86-64 doesn't just add two levels to page tables to support 64 bit
addresses, but is a different processor. For example, calling conventions,
system calls, and segmentation are different from 32-bit x86. Segmentation is
basically gone, but gs/fs in combination with MSRs can be used to hold a
per-core pointer. In general, x86-64 is more straightforward than 32-bit
x86. The port uses code from sv6 and the xv6 "rsc-amd64" branch.
A summary of the changes is as follows:
- Booting: switch to grub instead of xv6's bootloader (pass -kernel to qemu),
because xv6's boot loader doesn't understand 64bit ELF files. And, we don't
care anymore about booting.
- Makefile: use -m64 instead of -m32 flag for gcc, delete boot loader, xv6.img,
bochs, and memfs. For now dont' use -O2, since usertests with -O2 is bigger than
MAXFILE!
- Update gdb.tmpl to be for i386 or x86-64
- Console/printf: use stdarg.h and treat 64-bit addresses different from ints
(32-bit)
- Update elfhdr to be 64 bit
- entry.S/entryother.S: add code to switch to 64-bit mode: build a simple page
table in 32-bit mode before switching to 64-bit mode, share code for entering
boot processor and APs, and tweak boot gdt. The boot gdt is the gdt that the
kernel proper also uses. (In 64-bit mode, the gdt/segmentation and task state
mostly disappear.)
- exec.c: fix passing argv (64-bit now instead of 32-bit).
- initcode.c: use syscall instead of int.
- kernel.ld: load kernel very high, in top terabyte. 64 bits is a lot of
address space!
- proc.c: initial return is through new syscall path instead of trapret.
- proc.h: update struct cpu to have some scratch space since syscall saves less
state than int, update struct context to reflect x86-64 calling conventions.
- swtch: simplify for x86-64 calling conventions.
- syscall: add fetcharg to handle x86-64 calling convetions (6 arguments are
passed through registers), and fetchaddr to read a 64-bit value from user space.
- sysfile: update to handle pointers from user space (e.g., sys_exec), which are
64 bits.
- trap.c: no special trap vector for sys calls, because x86-64 has a different
plan for system calls.
- trapasm: one plan for syscalls and one plan for traps (interrupt and
exceptions). On x86-64, the kernel is responsible for switching user/kernel
stacks. To do, xv6 keeps some scratch space in the cpu structure, and uses MSR
GS_KERN_BASE to point to the core's cpu structure (using swapgs).
- types.h: add uint64, and change pde_t to uint64
- usertests: exit() when fork fails, which helped in tracking down one of the
bugs in the switch from 32-bit to 64-bit
- vectors: update to make them 64 bits
- vm.c: use bootgdt in kernel too, program MSRs for syscalls and core-local
state (for swapgs), walk 4 levels in walkpgdir, add DEVSPACETOP, use task
segment to set kernel stack for interrupts (but simpler than in 32-bit mode),
add an extra argument to freevm (size of user part of address space) to avoid
checking all entries till KERNBASE (there are MANY TB before the top 1TB).
- x86: update trapframe to have 64-bit entries, which is what the processor
pushes on syscalls and traps. simplify lgdt and lidt, using struct desctr,
which needs the gcc directives packed and aligned.
TODO:
- use int32 instead of int?
- simplify curproc(). xv6 has per-cpu state again, but this time it must have it.
- avoid repetition in walkpgdir
- fix validateint() in usertests.c
- fix bugs (e.g., observed one a case of entering kernel with invalid gs or proc
2018-09-23 12:24:42 +00:00
|
|
|
}
|
2006-07-11 17:39:45 +00:00
|
|
|
if(pid2 == 0)
|
2006-07-17 01:25:22 +00:00
|
|
|
for(;;)
|
2006-07-11 17:39:45 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
pipe(pfds);
|
|
|
|
pid3 = fork();
|
Checkpoint port of xv6 to x86-64. Passed usertests on 2 processors a few times.
The x86-64 doesn't just add two levels to page tables to support 64 bit
addresses, but is a different processor. For example, calling conventions,
system calls, and segmentation are different from 32-bit x86. Segmentation is
basically gone, but gs/fs in combination with MSRs can be used to hold a
per-core pointer. In general, x86-64 is more straightforward than 32-bit
x86. The port uses code from sv6 and the xv6 "rsc-amd64" branch.
A summary of the changes is as follows:
- Booting: switch to grub instead of xv6's bootloader (pass -kernel to qemu),
because xv6's boot loader doesn't understand 64bit ELF files. And, we don't
care anymore about booting.
- Makefile: use -m64 instead of -m32 flag for gcc, delete boot loader, xv6.img,
bochs, and memfs. For now dont' use -O2, since usertests with -O2 is bigger than
MAXFILE!
- Update gdb.tmpl to be for i386 or x86-64
- Console/printf: use stdarg.h and treat 64-bit addresses different from ints
(32-bit)
- Update elfhdr to be 64 bit
- entry.S/entryother.S: add code to switch to 64-bit mode: build a simple page
table in 32-bit mode before switching to 64-bit mode, share code for entering
boot processor and APs, and tweak boot gdt. The boot gdt is the gdt that the
kernel proper also uses. (In 64-bit mode, the gdt/segmentation and task state
mostly disappear.)
- exec.c: fix passing argv (64-bit now instead of 32-bit).
- initcode.c: use syscall instead of int.
- kernel.ld: load kernel very high, in top terabyte. 64 bits is a lot of
address space!
- proc.c: initial return is through new syscall path instead of trapret.
- proc.h: update struct cpu to have some scratch space since syscall saves less
state than int, update struct context to reflect x86-64 calling conventions.
- swtch: simplify for x86-64 calling conventions.
- syscall: add fetcharg to handle x86-64 calling convetions (6 arguments are
passed through registers), and fetchaddr to read a 64-bit value from user space.
- sysfile: update to handle pointers from user space (e.g., sys_exec), which are
64 bits.
- trap.c: no special trap vector for sys calls, because x86-64 has a different
plan for system calls.
- trapasm: one plan for syscalls and one plan for traps (interrupt and
exceptions). On x86-64, the kernel is responsible for switching user/kernel
stacks. To do, xv6 keeps some scratch space in the cpu structure, and uses MSR
GS_KERN_BASE to point to the core's cpu structure (using swapgs).
- types.h: add uint64, and change pde_t to uint64
- usertests: exit() when fork fails, which helped in tracking down one of the
bugs in the switch from 32-bit to 64-bit
- vectors: update to make them 64 bits
- vm.c: use bootgdt in kernel too, program MSRs for syscalls and core-local
state (for swapgs), walk 4 levels in walkpgdir, add DEVSPACETOP, use task
segment to set kernel stack for interrupts (but simpler than in 32-bit mode),
add an extra argument to freevm (size of user part of address space) to avoid
checking all entries till KERNBASE (there are MANY TB before the top 1TB).
- x86: update trapframe to have 64-bit entries, which is what the processor
pushes on syscalls and traps. simplify lgdt and lidt, using struct desctr,
which needs the gcc directives packed and aligned.
TODO:
- use int32 instead of int?
- simplify curproc(). xv6 has per-cpu state again, but this time it must have it.
- avoid repetition in walkpgdir
- fix validateint() in usertests.c
- fix bugs (e.g., observed one a case of entering kernel with invalid gs or proc
2018-09-23 12:24:42 +00:00
|
|
|
if(pid3 < 0) {
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: fork failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
Checkpoint port of xv6 to x86-64. Passed usertests on 2 processors a few times.
The x86-64 doesn't just add two levels to page tables to support 64 bit
addresses, but is a different processor. For example, calling conventions,
system calls, and segmentation are different from 32-bit x86. Segmentation is
basically gone, but gs/fs in combination with MSRs can be used to hold a
per-core pointer. In general, x86-64 is more straightforward than 32-bit
x86. The port uses code from sv6 and the xv6 "rsc-amd64" branch.
A summary of the changes is as follows:
- Booting: switch to grub instead of xv6's bootloader (pass -kernel to qemu),
because xv6's boot loader doesn't understand 64bit ELF files. And, we don't
care anymore about booting.
- Makefile: use -m64 instead of -m32 flag for gcc, delete boot loader, xv6.img,
bochs, and memfs. For now dont' use -O2, since usertests with -O2 is bigger than
MAXFILE!
- Update gdb.tmpl to be for i386 or x86-64
- Console/printf: use stdarg.h and treat 64-bit addresses different from ints
(32-bit)
- Update elfhdr to be 64 bit
- entry.S/entryother.S: add code to switch to 64-bit mode: build a simple page
table in 32-bit mode before switching to 64-bit mode, share code for entering
boot processor and APs, and tweak boot gdt. The boot gdt is the gdt that the
kernel proper also uses. (In 64-bit mode, the gdt/segmentation and task state
mostly disappear.)
- exec.c: fix passing argv (64-bit now instead of 32-bit).
- initcode.c: use syscall instead of int.
- kernel.ld: load kernel very high, in top terabyte. 64 bits is a lot of
address space!
- proc.c: initial return is through new syscall path instead of trapret.
- proc.h: update struct cpu to have some scratch space since syscall saves less
state than int, update struct context to reflect x86-64 calling conventions.
- swtch: simplify for x86-64 calling conventions.
- syscall: add fetcharg to handle x86-64 calling convetions (6 arguments are
passed through registers), and fetchaddr to read a 64-bit value from user space.
- sysfile: update to handle pointers from user space (e.g., sys_exec), which are
64 bits.
- trap.c: no special trap vector for sys calls, because x86-64 has a different
plan for system calls.
- trapasm: one plan for syscalls and one plan for traps (interrupt and
exceptions). On x86-64, the kernel is responsible for switching user/kernel
stacks. To do, xv6 keeps some scratch space in the cpu structure, and uses MSR
GS_KERN_BASE to point to the core's cpu structure (using swapgs).
- types.h: add uint64, and change pde_t to uint64
- usertests: exit() when fork fails, which helped in tracking down one of the
bugs in the switch from 32-bit to 64-bit
- vectors: update to make them 64 bits
- vm.c: use bootgdt in kernel too, program MSRs for syscalls and core-local
state (for swapgs), walk 4 levels in walkpgdir, add DEVSPACETOP, use task
segment to set kernel stack for interrupts (but simpler than in 32-bit mode),
add an extra argument to freevm (size of user part of address space) to avoid
checking all entries till KERNBASE (there are MANY TB before the top 1TB).
- x86: update trapframe to have 64-bit entries, which is what the processor
pushes on syscalls and traps. simplify lgdt and lidt, using struct desctr,
which needs the gcc directives packed and aligned.
TODO:
- use int32 instead of int?
- simplify curproc(). xv6 has per-cpu state again, but this time it must have it.
- avoid repetition in walkpgdir
- fix validateint() in usertests.c
- fix bugs (e.g., observed one a case of entering kernel with invalid gs or proc
2018-09-23 12:24:42 +00:00
|
|
|
}
|
2006-07-11 17:39:45 +00:00
|
|
|
if(pid3 == 0){
|
|
|
|
close(pfds[0]);
|
|
|
|
if(write(pfds[1], "x", 1) != 1)
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("%s: preempt write error", s);
|
2006-07-11 17:39:45 +00:00
|
|
|
close(pfds[1]);
|
2006-07-17 01:25:22 +00:00
|
|
|
for(;;)
|
2006-07-11 17:39:45 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(pfds[1]);
|
|
|
|
if(read(pfds[0], buf, sizeof(buf)) != 1){
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("%s: preempt read error", s);
|
2006-07-11 17:39:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
close(pfds[0]);
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("kill... ");
|
2006-07-11 17:39:45 +00:00
|
|
|
kill(pid1);
|
|
|
|
kill(pid2);
|
|
|
|
kill(pid3);
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("wait... ");
|
2019-09-10 16:30:10 +00:00
|
|
|
wait(0);
|
|
|
|
wait(0);
|
|
|
|
wait(0);
|
2006-07-11 17:39:45 +00:00
|
|
|
}
|
|
|
|
|
2006-07-15 12:03:57 +00:00
|
|
|
// try to find any races between exit and wait
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
exitwait(char *s)
|
2006-07-15 12:03:57 +00:00
|
|
|
{
|
|
|
|
int i, pid;
|
|
|
|
|
|
|
|
for(i = 0; i < 100; i++){
|
|
|
|
pid = fork();
|
|
|
|
if(pid < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: fork failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-07-15 12:03:57 +00:00
|
|
|
}
|
|
|
|
if(pid){
|
2019-09-10 16:30:10 +00:00
|
|
|
int xstate;
|
|
|
|
if(wait(&xstate) != pid){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: wait wrong pid\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2019-09-10 16:30:10 +00:00
|
|
|
}
|
|
|
|
if(i != xstate) {
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: wait wrong exit status\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-07-15 12:03:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(i);
|
2006-07-15 12:03:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 16:33:26 +00:00
|
|
|
// try to find races in the reparenting
|
|
|
|
// code that handles a parent exiting
|
|
|
|
// when it still has live children.
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
reparent(char *s)
|
2019-07-05 16:33:26 +00:00
|
|
|
{
|
|
|
|
int master_pid = getpid();
|
2019-07-25 10:30:49 +00:00
|
|
|
for(int i = 0; i < 200; i++){
|
2019-07-05 16:33:26 +00:00
|
|
|
int pid = fork();
|
|
|
|
if(pid < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: fork failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2019-07-05 16:33:26 +00:00
|
|
|
}
|
|
|
|
if(pid){
|
2019-09-10 16:30:10 +00:00
|
|
|
if(wait(0) != pid){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: wait wrong pid\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2019-07-05 16:33:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int pid2 = fork();
|
|
|
|
if(pid2 < 0){
|
|
|
|
kill(master_pid);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2019-07-05 16:33:26 +00:00
|
|
|
}
|
2019-09-19 10:58:28 +00:00
|
|
|
exit(0);
|
2019-07-05 16:33:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-19 10:58:28 +00:00
|
|
|
exit(0);
|
2019-07-05 16:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// what if two children exit() at the same time?
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
twochildren(char *s)
|
2019-07-05 16:33:26 +00:00
|
|
|
{
|
|
|
|
for(int i = 0; i < 1000; i++){
|
|
|
|
int pid1 = fork();
|
|
|
|
if(pid1 < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: fork failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2019-07-05 16:33:26 +00:00
|
|
|
}
|
|
|
|
if(pid1 == 0){
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2019-07-05 16:33:26 +00:00
|
|
|
} else {
|
|
|
|
int pid2 = fork();
|
|
|
|
if(pid2 < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: fork failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2019-07-05 16:33:26 +00:00
|
|
|
}
|
|
|
|
if(pid2 == 0){
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2019-07-05 16:33:26 +00:00
|
|
|
} else {
|
2019-09-10 16:30:10 +00:00
|
|
|
wait(0);
|
|
|
|
wait(0);
|
2019-07-05 16:33:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 09:41:59 +00:00
|
|
|
// concurrent forks to try to expose locking bugs.
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
forkfork(char *s)
|
2019-07-11 09:41:59 +00:00
|
|
|
{
|
2019-08-20 16:58:00 +00:00
|
|
|
enum { N=2 };
|
2019-07-11 09:41:59 +00:00
|
|
|
|
2019-08-20 16:58:00 +00:00
|
|
|
for(int i = 0; i < N; i++){
|
2019-07-11 09:41:59 +00:00
|
|
|
int pid = fork();
|
|
|
|
if(pid < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: fork failed", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2019-07-11 09:41:59 +00:00
|
|
|
}
|
|
|
|
if(pid == 0){
|
|
|
|
for(int j = 0; j < 200; j++){
|
|
|
|
int pid1 = fork();
|
|
|
|
if(pid1 < 0){
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2019-07-11 09:41:59 +00:00
|
|
|
}
|
|
|
|
if(pid1 == 0){
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2019-07-11 09:41:59 +00:00
|
|
|
}
|
2019-09-10 16:30:10 +00:00
|
|
|
wait(0);
|
2019-07-11 09:41:59 +00:00
|
|
|
}
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2019-07-11 09:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 10:58:28 +00:00
|
|
|
int xstatus;
|
2019-08-20 16:58:00 +00:00
|
|
|
for(int i = 0; i < N; i++){
|
2019-09-19 10:58:28 +00:00
|
|
|
wait(&xstatus);
|
|
|
|
if(xstatus != 0) {
|
|
|
|
printf("%s: fork in child failed", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
2019-07-11 09:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 16:33:26 +00:00
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
forkforkfork(char *s)
|
2019-07-05 16:33:26 +00:00
|
|
|
{
|
|
|
|
unlink("stopforking");
|
|
|
|
|
|
|
|
int pid = fork();
|
|
|
|
if(pid < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: fork failed", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2019-07-05 16:33:26 +00:00
|
|
|
}
|
|
|
|
if(pid == 0){
|
|
|
|
while(1){
|
|
|
|
int fd = open("stopforking", 0);
|
|
|
|
if(fd >= 0){
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2019-07-05 16:33:26 +00:00
|
|
|
}
|
|
|
|
if(fork() < 0){
|
|
|
|
close(open("stopforking", O_CREATE|O_RDWR));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2019-07-05 16:33:26 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 10:30:49 +00:00
|
|
|
sleep(20); // two seconds
|
2019-07-05 16:33:26 +00:00
|
|
|
close(open("stopforking", O_CREATE|O_RDWR));
|
2019-09-10 16:30:10 +00:00
|
|
|
wait(0);
|
2019-07-25 10:30:49 +00:00
|
|
|
sleep(10); // one second
|
2019-07-05 16:33:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 10:50:25 +00:00
|
|
|
// regression test. does reparent() violate the parent-then-child
|
|
|
|
// locking order when giving away a child to init, so that exit()
|
|
|
|
// deadlocks against init's wait()? also used to trigger a "panic:
|
|
|
|
// release" due to exit() releasing a different p->parent->lock than
|
|
|
|
// it acquired.
|
|
|
|
void
|
|
|
|
reparent2(char *s)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < 800; i++){
|
|
|
|
int pid1 = fork();
|
|
|
|
if(pid1 < 0){
|
|
|
|
printf("fork failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(pid1 == 0){
|
|
|
|
fork();
|
|
|
|
fork();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
wait(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2019-09-19 19:22:45 +00:00
|
|
|
// allocate all mem, free it, and allocate again
|
2006-08-24 19:24:36 +00:00
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
mem(char *s)
|
2006-08-24 19:24:36 +00:00
|
|
|
{
|
|
|
|
void *m1, *m2;
|
2019-09-19 10:58:28 +00:00
|
|
|
int pid;
|
2006-08-24 19:24:36 +00:00
|
|
|
|
2006-08-29 19:06:37 +00:00
|
|
|
if((pid = fork()) == 0){
|
|
|
|
m1 = 0;
|
2011-01-11 18:01:13 +00:00
|
|
|
while((m2 = malloc(10001)) != 0){
|
|
|
|
*(char**)m2 = m1;
|
2006-08-29 19:06:37 +00:00
|
|
|
m1 = m2;
|
|
|
|
}
|
2011-01-11 18:01:13 +00:00
|
|
|
while(m1){
|
2006-09-06 17:27:19 +00:00
|
|
|
m2 = *(char**)m1;
|
2006-08-29 19:06:37 +00:00
|
|
|
free(m1);
|
|
|
|
m1 = m2;
|
|
|
|
}
|
|
|
|
m1 = malloc(1024*20);
|
2011-01-11 18:01:13 +00:00
|
|
|
if(m1 == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("couldn't allocate mem?!!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-08-29 19:06:37 +00:00
|
|
|
}
|
2006-08-24 19:24:36 +00:00
|
|
|
free(m1);
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2006-08-29 19:06:37 +00:00
|
|
|
} else {
|
2019-09-19 10:58:28 +00:00
|
|
|
int xstatus;
|
|
|
|
wait(&xstatus);
|
2020-08-27 10:21:10 +00:00
|
|
|
if(xstatus == -1){
|
|
|
|
// probably page fault, so might be lazy lab,
|
|
|
|
// so OK.
|
|
|
|
exit(0);
|
|
|
|
}
|
2019-09-19 10:58:28 +00:00
|
|
|
exit(xstatus);
|
2006-08-24 19:24:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-07 13:23:41 +00:00
|
|
|
// More file system tests
|
|
|
|
|
|
|
|
// two processes write to the same file descriptor
|
|
|
|
// is the offset shared? does inode locking work?
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
sharedfd(char *s)
|
2006-09-07 13:23:41 +00:00
|
|
|
{
|
|
|
|
int fd, pid, i, n, nc, np;
|
2019-08-20 16:58:00 +00:00
|
|
|
enum { N = 1000, SZ=10};
|
|
|
|
char buf[SZ];
|
2006-09-07 13:23:41 +00:00
|
|
|
|
|
|
|
unlink("sharedfd");
|
|
|
|
fd = open("sharedfd", O_CREATE|O_RDWR);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: cannot open sharedfd for writing", s);
|
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
pid = fork();
|
|
|
|
memset(buf, pid==0?'c':'p', sizeof(buf));
|
2019-08-20 16:58:00 +00:00
|
|
|
for(i = 0; i < N; i++){
|
2006-09-07 13:23:41 +00:00
|
|
|
if(write(fd, buf, sizeof(buf)) != sizeof(buf)){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: write sharedfd failed\n", s);
|
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-19 10:58:28 +00:00
|
|
|
if(pid == 0) {
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2019-09-19 10:58:28 +00:00
|
|
|
} else {
|
|
|
|
int xstatus;
|
|
|
|
wait(&xstatus);
|
|
|
|
if(xstatus != 0)
|
|
|
|
exit(xstatus);
|
|
|
|
}
|
|
|
|
|
2006-09-07 13:23:41 +00:00
|
|
|
close(fd);
|
|
|
|
fd = open("sharedfd", 0);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: cannot open sharedfd for reading\n", s);
|
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
nc = np = 0;
|
|
|
|
while((n = read(fd, buf, sizeof(buf))) > 0){
|
|
|
|
for(i = 0; i < sizeof(buf); i++){
|
|
|
|
if(buf[i] == 'c')
|
|
|
|
nc++;
|
|
|
|
if(buf[i] == 'p')
|
|
|
|
np++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
unlink("sharedfd");
|
2019-08-20 16:58:00 +00:00
|
|
|
if(nc == N*SZ && np == N*SZ){
|
2019-09-19 10:58:28 +00:00
|
|
|
exit(0);
|
2011-08-31 00:50:19 +00:00
|
|
|
} else {
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: nc/np test fails\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2011-08-31 00:50:19 +00:00
|
|
|
}
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
2014-08-28 09:57:47 +00:00
|
|
|
// four processes write different files at the same
|
2006-09-07 13:23:41 +00:00
|
|
|
// time, to test block allocation.
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
fourfiles(char *s)
|
2006-09-07 13:23:41 +00:00
|
|
|
{
|
2014-08-28 09:57:47 +00:00
|
|
|
int fd, pid, i, j, n, total, pi;
|
|
|
|
char *names[] = { "f0", "f1", "f2", "f3" };
|
2006-09-07 13:23:41 +00:00
|
|
|
char *fname;
|
2019-08-20 16:58:00 +00:00
|
|
|
enum { N=12, NCHILD=4, SZ=500 };
|
|
|
|
|
|
|
|
for(pi = 0; pi < NCHILD; pi++){
|
2014-08-28 09:57:47 +00:00
|
|
|
fname = names[pi];
|
|
|
|
unlink(fname);
|
2006-09-07 13:23:41 +00:00
|
|
|
|
2014-08-28 09:57:47 +00:00
|
|
|
pid = fork();
|
|
|
|
if(pid < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("fork failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-28 09:57:47 +00:00
|
|
|
}
|
2006-09-07 13:23:41 +00:00
|
|
|
|
2014-08-28 09:57:47 +00:00
|
|
|
if(pid == 0){
|
|
|
|
fd = open(fname, O_CREATE | O_RDWR);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("create failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-28 09:57:47 +00:00
|
|
|
}
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2019-08-20 16:58:00 +00:00
|
|
|
memset(buf, '0'+pi, SZ);
|
|
|
|
for(i = 0; i < N; i++){
|
|
|
|
if((n = write(fd, buf, SZ)) != SZ){
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("write failed %d\n", n);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-28 09:57:47 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-28 09:57:47 +00:00
|
|
|
|
2019-09-19 10:58:28 +00:00
|
|
|
int xstatus;
|
2019-08-20 16:58:00 +00:00
|
|
|
for(pi = 0; pi < NCHILD; pi++){
|
2019-09-19 10:58:28 +00:00
|
|
|
wait(&xstatus);
|
|
|
|
if(xstatus != 0)
|
|
|
|
exit(xstatus);
|
2014-08-28 09:57:47 +00:00
|
|
|
}
|
2006-09-07 13:23:41 +00:00
|
|
|
|
2019-08-20 16:58:00 +00:00
|
|
|
for(i = 0; i < NCHILD; i++){
|
2014-08-28 09:57:47 +00:00
|
|
|
fname = names[i];
|
|
|
|
fd = open(fname, 0);
|
2006-09-07 13:23:41 +00:00
|
|
|
total = 0;
|
|
|
|
while((n = read(fd, buf, sizeof(buf))) > 0){
|
|
|
|
for(j = 0; j < n; j++){
|
2014-08-28 09:57:47 +00:00
|
|
|
if(buf[j] != '0'+i){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("wrong char\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
total += n;
|
|
|
|
}
|
|
|
|
close(fd);
|
2019-08-20 16:58:00 +00:00
|
|
|
if(total != N*SZ){
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("wrong length %d\n", total);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2014-08-28 09:57:47 +00:00
|
|
|
unlink(fname);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-28 09:57:47 +00:00
|
|
|
// four processes create and delete different files in same directory
|
2006-09-07 13:23:41 +00:00
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
createdelete(char *s)
|
2006-09-07 13:23:41 +00:00
|
|
|
{
|
2018-09-29 12:29:34 +00:00
|
|
|
enum { N = 20, NCHILD=4 };
|
2014-08-28 09:57:47 +00:00
|
|
|
int pid, i, fd, pi;
|
2006-09-07 13:23:41 +00:00
|
|
|
char name[32];
|
|
|
|
|
2018-09-29 12:29:34 +00:00
|
|
|
for(pi = 0; pi < NCHILD; pi++){
|
2014-08-28 09:57:47 +00:00
|
|
|
pid = fork();
|
|
|
|
if(pid < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("fork failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2014-08-28 09:57:47 +00:00
|
|
|
|
|
|
|
if(pid == 0){
|
|
|
|
name[0] = 'p' + pi;
|
|
|
|
name[2] = '\0';
|
|
|
|
for(i = 0; i < N; i++){
|
|
|
|
name[1] = '0' + i;
|
|
|
|
fd = open(name, O_CREATE | O_RDWR);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: create failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-28 09:57:47 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
if(i > 0 && (i % 2 ) == 0){
|
|
|
|
name[1] = '0' + (i / 2);
|
|
|
|
if(unlink(name) < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-28 09:57:47 +00:00
|
|
|
}
|
|
|
|
}
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 10:58:28 +00:00
|
|
|
int xstatus;
|
2018-09-29 12:29:34 +00:00
|
|
|
for(pi = 0; pi < NCHILD; pi++){
|
2019-09-19 10:58:28 +00:00
|
|
|
wait(&xstatus);
|
|
|
|
if(xstatus != 0)
|
|
|
|
exit(1);
|
2014-08-28 09:57:47 +00:00
|
|
|
}
|
2006-09-07 13:23:41 +00:00
|
|
|
|
2014-08-28 09:57:47 +00:00
|
|
|
name[0] = name[1] = name[2] = 0;
|
2007-08-10 17:17:42 +00:00
|
|
|
for(i = 0; i < N; i++){
|
2018-09-29 12:29:34 +00:00
|
|
|
for(pi = 0; pi < NCHILD; pi++){
|
2014-08-28 09:57:47 +00:00
|
|
|
name[0] = 'p' + pi;
|
|
|
|
name[1] = '0' + i;
|
|
|
|
fd = open(name, 0);
|
|
|
|
if((i == 0 || i >= N/2) && fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: oops createdelete %s didn't exist\n", s, name);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-28 09:57:47 +00:00
|
|
|
} else if((i >= 1 && i < N/2) && fd >= 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: oops createdelete %s did exist\n", s, name);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2014-08-28 09:57:47 +00:00
|
|
|
}
|
|
|
|
if(fd >= 0)
|
|
|
|
close(fd);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-10 17:17:42 +00:00
|
|
|
for(i = 0; i < N; i++){
|
2018-09-29 12:29:34 +00:00
|
|
|
for(pi = 0; pi < NCHILD; pi++){
|
2014-08-28 09:57:47 +00:00
|
|
|
name[0] = 'p' + i;
|
|
|
|
name[1] = '0' + i;
|
|
|
|
unlink(name);
|
|
|
|
}
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// can I unlink a file and still read it?
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
unlinkread(char *s)
|
2006-09-07 13:23:41 +00:00
|
|
|
{
|
2019-08-20 16:58:00 +00:00
|
|
|
enum { SZ = 5 };
|
2006-09-07 13:23:41 +00:00
|
|
|
int fd, fd1;
|
|
|
|
|
|
|
|
fd = open("unlinkread", O_CREATE | O_RDWR);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: create unlinkread failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2019-08-20 16:58:00 +00:00
|
|
|
write(fd, "hello", SZ);
|
2006-09-07 13:23:41 +00:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
fd = open("unlinkread", O_RDWR);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: open unlinkread failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(unlink("unlinkread") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink unlinkread failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fd1 = open("unlinkread", O_CREATE | O_RDWR);
|
|
|
|
write(fd1, "yyy", 3);
|
|
|
|
close(fd1);
|
|
|
|
|
2019-08-20 16:58:00 +00:00
|
|
|
if(read(fd, buf, sizeof(buf)) != SZ){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlinkread read failed", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(buf[0] != 'h'){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlinkread wrong data\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(write(fd, buf, 10) != 10){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlinkread write failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
unlink("unlinkread");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
linktest(char *s)
|
2006-09-07 13:23:41 +00:00
|
|
|
{
|
2019-08-20 16:58:00 +00:00
|
|
|
enum { SZ = 5 };
|
2006-09-07 13:23:41 +00:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
unlink("lf1");
|
|
|
|
unlink("lf2");
|
|
|
|
|
|
|
|
fd = open("lf1", O_CREATE|O_RDWR);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: create lf1 failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2019-08-20 16:58:00 +00:00
|
|
|
if(write(fd, "hello", SZ) != SZ){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: write lf1 failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if(link("lf1", "lf2") < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: link lf1 lf2 failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
unlink("lf1");
|
|
|
|
|
|
|
|
if(open("lf1", 0) >= 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlinked lf1 but it is still there!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fd = open("lf2", 0);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: open lf2 failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2019-08-20 16:58:00 +00:00
|
|
|
if(read(fd, buf, sizeof(buf)) != SZ){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: read lf2 failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if(link("lf2", "lf2") >= 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: link lf2 lf2 succeeded! oops\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unlink("lf2");
|
|
|
|
if(link("lf2", "lf1") >= 0){
|
2021-05-18 03:29:42 +00:00
|
|
|
printf("%s: link non-existent succeeded! oops\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(link(".", "lf1") >= 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: link . lf1 succeeded! oops\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-15 16:44:20 +00:00
|
|
|
// test concurrent create/link/unlink of the same file
|
2006-09-07 13:23:41 +00:00
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
concreate(char *s)
|
2006-09-07 13:23:41 +00:00
|
|
|
{
|
2019-08-20 16:58:00 +00:00
|
|
|
enum { N = 40 };
|
2006-09-07 13:23:41 +00:00
|
|
|
char file[3];
|
|
|
|
int i, pid, n, fd;
|
2019-08-20 16:58:00 +00:00
|
|
|
char fa[N];
|
2006-09-07 13:23:41 +00:00
|
|
|
struct {
|
2007-08-08 09:30:48 +00:00
|
|
|
ushort inum;
|
2019-08-20 16:58:00 +00:00
|
|
|
char name[DIRSIZ];
|
2006-09-07 13:23:41 +00:00
|
|
|
} de;
|
|
|
|
|
|
|
|
file[0] = 'C';
|
|
|
|
file[2] = '\0';
|
2019-08-20 16:58:00 +00:00
|
|
|
for(i = 0; i < N; i++){
|
2006-09-07 13:23:41 +00:00
|
|
|
file[1] = '0' + i;
|
|
|
|
unlink(file);
|
|
|
|
pid = fork();
|
|
|
|
if(pid && (i % 3) == 1){
|
|
|
|
link("C0", file);
|
|
|
|
} else if(pid == 0 && (i % 5) == 1){
|
|
|
|
link("C0", file);
|
|
|
|
} else {
|
|
|
|
fd = open(file, O_CREATE | O_RDWR);
|
|
|
|
if(fd < 0){
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("concreate create %s failed\n", file);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
2019-09-19 10:58:28 +00:00
|
|
|
if(pid == 0) {
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2019-09-19 10:58:28 +00:00
|
|
|
} else {
|
|
|
|
int xstatus;
|
|
|
|
wait(&xstatus);
|
|
|
|
if(xstatus != 0)
|
|
|
|
exit(1);
|
|
|
|
}
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(fa, 0, sizeof(fa));
|
|
|
|
fd = open(".", 0);
|
|
|
|
n = 0;
|
|
|
|
while(read(fd, &de, sizeof(de)) > 0){
|
|
|
|
if(de.inum == 0)
|
|
|
|
continue;
|
|
|
|
if(de.name[0] == 'C' && de.name[2] == '\0'){
|
|
|
|
i = de.name[1] - '0';
|
|
|
|
if(i < 0 || i >= sizeof(fa)){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: concreate weird file %s\n", s, de.name);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(fa[i]){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: concreate duplicate file %s\n", s, de.name);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
fa[i] = 1;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
2019-08-20 16:58:00 +00:00
|
|
|
if(n != N){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: concreate not enough files in directory listing\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 16:58:00 +00:00
|
|
|
for(i = 0; i < N; i++){
|
2006-09-07 13:23:41 +00:00
|
|
|
file[1] = '0' + i;
|
2007-08-24 12:19:13 +00:00
|
|
|
pid = fork();
|
|
|
|
if(pid < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: fork failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-24 12:19:13 +00:00
|
|
|
}
|
|
|
|
if(((i % 3) == 0 && pid == 0) ||
|
|
|
|
((i % 3) == 1 && pid != 0)){
|
2011-08-15 16:44:20 +00:00
|
|
|
close(open(file, 0));
|
|
|
|
close(open(file, 0));
|
|
|
|
close(open(file, 0));
|
|
|
|
close(open(file, 0));
|
2019-11-06 16:18:43 +00:00
|
|
|
close(open(file, 0));
|
|
|
|
close(open(file, 0));
|
2007-08-24 12:19:13 +00:00
|
|
|
} else {
|
|
|
|
unlink(file);
|
2011-08-15 16:44:20 +00:00
|
|
|
unlink(file);
|
|
|
|
unlink(file);
|
|
|
|
unlink(file);
|
2019-11-06 16:18:43 +00:00
|
|
|
unlink(file);
|
|
|
|
unlink(file);
|
2007-08-24 12:19:13 +00:00
|
|
|
}
|
|
|
|
if(pid == 0)
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2007-08-24 12:19:13 +00:00
|
|
|
else
|
2019-09-10 16:30:10 +00:00
|
|
|
wait(0);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-15 16:44:20 +00:00
|
|
|
// another concurrent link/unlink/create test,
|
|
|
|
// to look for deadlocks.
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
linkunlink(char *s)
|
2011-08-15 16:44:20 +00:00
|
|
|
{
|
|
|
|
int pid, i;
|
|
|
|
|
|
|
|
unlink("x");
|
|
|
|
pid = fork();
|
|
|
|
if(pid < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: fork failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2011-08-15 16:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int x = (pid ? 1 : 97);
|
|
|
|
for(i = 0; i < 100; i++){
|
|
|
|
x = x * 1103515245 + 12345;
|
|
|
|
if((x % 3) == 0){
|
|
|
|
close(open("x", O_RDWR | O_CREATE));
|
|
|
|
} else if((x % 3) == 1){
|
|
|
|
link("cat", "x");
|
|
|
|
} else {
|
|
|
|
unlink("x");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pid)
|
2019-09-10 16:30:10 +00:00
|
|
|
wait(0);
|
2016-08-25 13:13:00 +00:00
|
|
|
else
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2011-08-15 16:44:20 +00:00
|
|
|
}
|
|
|
|
|
2006-09-07 13:23:41 +00:00
|
|
|
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
subdir(char *s)
|
2006-09-07 13:23:41 +00:00
|
|
|
{
|
|
|
|
int fd, cc;
|
|
|
|
|
|
|
|
unlink("ff");
|
|
|
|
if(mkdir("dd") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: mkdir dd failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fd = open("dd/ff", O_CREATE | O_RDWR);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: create dd/ff failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
write(fd, "ff", 2);
|
|
|
|
close(fd);
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-22 02:21:22 +00:00
|
|
|
if(unlink("dd") >= 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink dd (non-empty dir) succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-22 02:21:22 +00:00
|
|
|
}
|
2006-09-07 13:23:41 +00:00
|
|
|
|
|
|
|
if(mkdir("/dd/dd") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("subdir mkdir dd/dd failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fd = open("dd/dd/ff", O_CREATE | O_RDWR);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: create dd/dd/ff failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
write(fd, "FF", 2);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
fd = open("dd/dd/../ff", 0);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: open dd/dd/../ff failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
cc = read(fd, buf, sizeof(buf));
|
|
|
|
if(cc != 2 || buf[0] != 'f'){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: dd/dd/../ff wrong content\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if(link("dd/dd/ff", "dd/dd/ffff") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("link dd/dd/ff dd/dd/ffff failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(unlink("dd/dd/ff") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink dd/dd/ff failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2007-08-22 02:21:22 +00:00
|
|
|
if(open("dd/dd/ff", O_RDONLY) >= 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: open (unlinked) dd/dd/ff succeeded\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-22 02:21:22 +00:00
|
|
|
}
|
2006-09-07 13:23:41 +00:00
|
|
|
|
|
|
|
if(chdir("dd") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: chdir dd failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(chdir("dd/../../dd") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: chdir dd/../../dd failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2007-08-22 02:21:22 +00:00
|
|
|
if(chdir("dd/../../../dd") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("chdir dd/../../dd failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-22 02:21:22 +00:00
|
|
|
}
|
2006-09-07 13:23:41 +00:00
|
|
|
if(chdir("./..") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: chdir ./.. failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fd = open("dd/dd/ffff", 0);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: open dd/dd/ffff failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(read(fd, buf, sizeof(buf)) != 2){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: read dd/dd/ffff wrong len\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
2007-08-22 02:21:22 +00:00
|
|
|
if(open("dd/dd/ff", O_RDONLY) >= 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: open (unlinked) dd/dd/ff succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(open("dd/ff/ff", O_CREATE|O_RDWR) >= 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: create dd/ff/ff succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(open("dd/xx/ff", O_CREATE|O_RDWR) >= 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: create dd/xx/ff succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(open("dd", O_CREATE) >= 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: create dd succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(open("dd", O_RDWR) >= 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: open dd rdwr succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(open("dd", O_WRONLY) >= 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: open dd wronly succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(link("dd/ff/ff", "dd/dd/xx") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: link dd/ff/ff dd/dd/xx succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(link("dd/xx/ff", "dd/dd/xx") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: link dd/xx/ff dd/dd/xx succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(link("dd/ff", "dd/dd/ffff") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: link dd/ff dd/dd/ffff succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(mkdir("dd/ff/ff") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: mkdir dd/ff/ff succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(mkdir("dd/xx/ff") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: mkdir dd/xx/ff succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(mkdir("dd/dd/ffff") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: mkdir dd/dd/ffff succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(unlink("dd/xx/ff") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink dd/xx/ff succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(unlink("dd/ff/ff") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink dd/ff/ff succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(chdir("dd/ff") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: chdir dd/ff succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(chdir("dd/xx") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: chdir dd/xx succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(unlink("dd/dd/ffff") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink dd/dd/ff failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(unlink("dd/ff") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink dd/ff failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2007-08-22 02:21:22 +00:00
|
|
|
if(unlink("dd") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink non-empty dd succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-22 02:21:22 +00:00
|
|
|
}
|
|
|
|
if(unlink("dd/dd") < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink dd/dd failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-22 02:21:22 +00:00
|
|
|
}
|
|
|
|
if(unlink("dd") < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink dd failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-22 02:21:22 +00:00
|
|
|
}
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
2011-08-12 13:25:39 +00:00
|
|
|
// test writes that are larger than the log.
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
bigwrite(char *s)
|
2011-08-12 13:25:39 +00:00
|
|
|
{
|
|
|
|
int fd, sz;
|
|
|
|
|
|
|
|
unlink("bigwrite");
|
2019-08-20 16:58:00 +00:00
|
|
|
for(sz = 499; sz < (MAXOPBLOCKS+2)*BSIZE; sz += 471){
|
2011-08-12 13:25:39 +00:00
|
|
|
fd = open("bigwrite", O_CREATE | O_RDWR);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: cannot create bigwrite\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2011-08-12 13:25:39 +00:00
|
|
|
}
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < 2; i++){
|
|
|
|
int cc = write(fd, buf, sz);
|
|
|
|
if(cc != sz){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: write(%d) ret %d\n", s, sz, cc);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2011-08-12 13:25:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
unlink("bigwrite");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-04 12:44:32 +00:00
|
|
|
|
2006-09-07 13:23:41 +00:00
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
bigfile(char *s)
|
2006-09-07 13:23:41 +00:00
|
|
|
{
|
2019-08-20 16:58:00 +00:00
|
|
|
enum { N = 20, SZ=600 };
|
2006-09-07 13:23:41 +00:00
|
|
|
int fd, i, total, cc;
|
|
|
|
|
2019-11-06 16:18:43 +00:00
|
|
|
unlink("bigfile.dat");
|
|
|
|
fd = open("bigfile.dat", O_CREATE | O_RDWR);
|
2006-09-07 13:23:41 +00:00
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: cannot create bigfile", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2019-08-20 16:58:00 +00:00
|
|
|
for(i = 0; i < N; i++){
|
|
|
|
memset(buf, i, SZ);
|
|
|
|
if(write(fd, buf, SZ) != SZ){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: write bigfile failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
2019-11-06 16:18:43 +00:00
|
|
|
fd = open("bigfile.dat", 0);
|
2006-09-07 13:23:41 +00:00
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: cannot open bigfile\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
total = 0;
|
|
|
|
for(i = 0; ; i++){
|
2019-08-20 16:58:00 +00:00
|
|
|
cc = read(fd, buf, SZ/2);
|
2006-09-07 13:23:41 +00:00
|
|
|
if(cc < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: read bigfile failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(cc == 0)
|
|
|
|
break;
|
2019-08-20 16:58:00 +00:00
|
|
|
if(cc != SZ/2){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: short read bigfile\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2019-08-20 16:58:00 +00:00
|
|
|
if(buf[0] != i/2 || buf[SZ/2-1] != i/2){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: read bigfile wrong data\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
total += cc;
|
|
|
|
}
|
|
|
|
close(fd);
|
2019-08-20 16:58:00 +00:00
|
|
|
if(total != N*SZ){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: read bigfile wrong total\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2019-11-06 16:18:43 +00:00
|
|
|
unlink("bigfile.dat");
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
fourteen(char *s)
|
2006-09-07 13:23:41 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2007-08-10 17:53:09 +00:00
|
|
|
// DIRSIZ is 14.
|
2006-09-07 13:23:41 +00:00
|
|
|
|
|
|
|
if(mkdir("12345678901234") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: mkdir 12345678901234 failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(mkdir("12345678901234/123456789012345") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: mkdir 12345678901234/123456789012345 failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
fd = open("123456789012345/123456789012345/123456789012345", O_CREATE);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: create 123456789012345/123456789012345/123456789012345 failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
fd = open("12345678901234/12345678901234/12345678901234", 0);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: open 12345678901234/12345678901234/12345678901234 failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if(mkdir("12345678901234/12345678901234") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: mkdir 12345678901234/12345678901234 succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
if(mkdir("123456789012345/12345678901234") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: mkdir 12345678901234/123456789012345 succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
2019-10-28 09:58:28 +00:00
|
|
|
|
|
|
|
// clean up
|
|
|
|
unlink("123456789012345/12345678901234");
|
|
|
|
unlink("12345678901234/12345678901234");
|
|
|
|
unlink("12345678901234/12345678901234/12345678901234");
|
|
|
|
unlink("123456789012345/123456789012345/123456789012345");
|
|
|
|
unlink("12345678901234/123456789012345");
|
|
|
|
unlink("12345678901234");
|
2006-09-07 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
2007-08-10 17:53:09 +00:00
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
rmdot(char *s)
|
2007-08-10 17:53:09 +00:00
|
|
|
{
|
|
|
|
if(mkdir("dots") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: mkdir dots failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-10 17:53:09 +00:00
|
|
|
}
|
|
|
|
if(chdir("dots") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: chdir dots failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-10 17:53:09 +00:00
|
|
|
}
|
|
|
|
if(unlink(".") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: rm . worked!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-10 17:53:09 +00:00
|
|
|
}
|
|
|
|
if(unlink("..") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: rm .. worked!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-10 17:53:09 +00:00
|
|
|
}
|
|
|
|
if(chdir("/") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: chdir / failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-10 17:53:09 +00:00
|
|
|
}
|
|
|
|
if(unlink("dots/.") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink dots/. worked!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-10 17:53:09 +00:00
|
|
|
}
|
|
|
|
if(unlink("dots/..") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink dots/.. worked!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-10 17:53:09 +00:00
|
|
|
}
|
|
|
|
if(unlink("dots") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink dots failed!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-10 17:53:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-24 14:56:17 +00:00
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
dirfile(char *s)
|
2007-08-24 14:56:17 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open("dirfile", O_CREATE);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: create dirfile failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-24 14:56:17 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
if(chdir("dirfile") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: chdir dirfile succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-24 14:56:17 +00:00
|
|
|
}
|
|
|
|
fd = open("dirfile/xx", 0);
|
|
|
|
if(fd >= 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: create dirfile/xx succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-24 14:56:17 +00:00
|
|
|
}
|
|
|
|
fd = open("dirfile/xx", O_CREATE);
|
|
|
|
if(fd >= 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: create dirfile/xx succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-24 14:56:17 +00:00
|
|
|
}
|
|
|
|
if(mkdir("dirfile/xx") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: mkdir dirfile/xx succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-24 14:56:17 +00:00
|
|
|
}
|
|
|
|
if(unlink("dirfile/xx") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink dirfile/xx succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-24 14:56:17 +00:00
|
|
|
}
|
|
|
|
if(link("README", "dirfile/xx") == 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: link to dirfile/xx succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-24 14:56:17 +00:00
|
|
|
}
|
|
|
|
if(unlink("dirfile") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: unlink dirfile failed!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-24 14:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(".", O_RDWR);
|
|
|
|
if(fd >= 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: open . for writing succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-24 14:56:17 +00:00
|
|
|
}
|
|
|
|
fd = open(".", 0);
|
|
|
|
if(write(fd, "x", 1) > 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: write . succeeded!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-24 14:56:17 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2019-10-28 09:58:28 +00:00
|
|
|
// test that iput() is called at the end of _namei().
|
|
|
|
// also tests empty file names.
|
2007-08-24 14:56:17 +00:00
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
iref(char *s)
|
2007-08-24 14:56:17 +00:00
|
|
|
{
|
|
|
|
int i, fd;
|
|
|
|
|
2019-08-20 16:58:00 +00:00
|
|
|
for(i = 0; i < NINODE + 1; i++){
|
2007-08-24 14:56:17 +00:00
|
|
|
if(mkdir("irefd") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: mkdir irefd failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-24 14:56:17 +00:00
|
|
|
}
|
|
|
|
if(chdir("irefd") != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: chdir irefd failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-24 14:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mkdir("");
|
|
|
|
link("README", "");
|
|
|
|
fd = open("", O_CREATE);
|
|
|
|
if(fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
fd = open("xx", O_CREATE);
|
|
|
|
if(fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
unlink("xx");
|
|
|
|
}
|
|
|
|
|
2019-10-28 09:58:28 +00:00
|
|
|
// clean up
|
|
|
|
for(i = 0; i < NINODE + 1; i++){
|
|
|
|
chdir("..");
|
|
|
|
unlink("irefd");
|
|
|
|
}
|
|
|
|
|
2007-08-24 14:56:17 +00:00
|
|
|
chdir("/");
|
|
|
|
}
|
|
|
|
|
2007-08-24 20:20:23 +00:00
|
|
|
// test that fork fails gracefully
|
|
|
|
// the forktest binary also does this, but it runs out of proc entries first.
|
|
|
|
// inside the bigger usertests binary, we run out of memory first.
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
forktest(char *s)
|
2007-08-24 20:20:23 +00:00
|
|
|
{
|
2019-08-20 16:58:00 +00:00
|
|
|
enum{ N = 1000 };
|
2007-08-24 20:20:23 +00:00
|
|
|
int n, pid;
|
|
|
|
|
2019-08-20 16:58:00 +00:00
|
|
|
for(n=0; n<N; n++){
|
2007-08-24 20:20:23 +00:00
|
|
|
pid = fork();
|
|
|
|
if(pid < 0)
|
|
|
|
break;
|
|
|
|
if(pid == 0)
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2007-08-24 20:20:23 +00:00
|
|
|
}
|
2016-08-25 13:13:00 +00:00
|
|
|
|
Checkpoint port of xv6 to x86-64. Passed usertests on 2 processors a few times.
The x86-64 doesn't just add two levels to page tables to support 64 bit
addresses, but is a different processor. For example, calling conventions,
system calls, and segmentation are different from 32-bit x86. Segmentation is
basically gone, but gs/fs in combination with MSRs can be used to hold a
per-core pointer. In general, x86-64 is more straightforward than 32-bit
x86. The port uses code from sv6 and the xv6 "rsc-amd64" branch.
A summary of the changes is as follows:
- Booting: switch to grub instead of xv6's bootloader (pass -kernel to qemu),
because xv6's boot loader doesn't understand 64bit ELF files. And, we don't
care anymore about booting.
- Makefile: use -m64 instead of -m32 flag for gcc, delete boot loader, xv6.img,
bochs, and memfs. For now dont' use -O2, since usertests with -O2 is bigger than
MAXFILE!
- Update gdb.tmpl to be for i386 or x86-64
- Console/printf: use stdarg.h and treat 64-bit addresses different from ints
(32-bit)
- Update elfhdr to be 64 bit
- entry.S/entryother.S: add code to switch to 64-bit mode: build a simple page
table in 32-bit mode before switching to 64-bit mode, share code for entering
boot processor and APs, and tweak boot gdt. The boot gdt is the gdt that the
kernel proper also uses. (In 64-bit mode, the gdt/segmentation and task state
mostly disappear.)
- exec.c: fix passing argv (64-bit now instead of 32-bit).
- initcode.c: use syscall instead of int.
- kernel.ld: load kernel very high, in top terabyte. 64 bits is a lot of
address space!
- proc.c: initial return is through new syscall path instead of trapret.
- proc.h: update struct cpu to have some scratch space since syscall saves less
state than int, update struct context to reflect x86-64 calling conventions.
- swtch: simplify for x86-64 calling conventions.
- syscall: add fetcharg to handle x86-64 calling convetions (6 arguments are
passed through registers), and fetchaddr to read a 64-bit value from user space.
- sysfile: update to handle pointers from user space (e.g., sys_exec), which are
64 bits.
- trap.c: no special trap vector for sys calls, because x86-64 has a different
plan for system calls.
- trapasm: one plan for syscalls and one plan for traps (interrupt and
exceptions). On x86-64, the kernel is responsible for switching user/kernel
stacks. To do, xv6 keeps some scratch space in the cpu structure, and uses MSR
GS_KERN_BASE to point to the core's cpu structure (using swapgs).
- types.h: add uint64, and change pde_t to uint64
- usertests: exit() when fork fails, which helped in tracking down one of the
bugs in the switch from 32-bit to 64-bit
- vectors: update to make them 64 bits
- vm.c: use bootgdt in kernel too, program MSRs for syscalls and core-local
state (for swapgs), walk 4 levels in walkpgdir, add DEVSPACETOP, use task
segment to set kernel stack for interrupts (but simpler than in 32-bit mode),
add an extra argument to freevm (size of user part of address space) to avoid
checking all entries till KERNBASE (there are MANY TB before the top 1TB).
- x86: update trapframe to have 64-bit entries, which is what the processor
pushes on syscalls and traps. simplify lgdt and lidt, using struct desctr,
which needs the gcc directives packed and aligned.
TODO:
- use int32 instead of int?
- simplify curproc(). xv6 has per-cpu state again, but this time it must have it.
- avoid repetition in walkpgdir
- fix validateint() in usertests.c
- fix bugs (e.g., observed one a case of entering kernel with invalid gs or proc
2018-09-23 12:24:42 +00:00
|
|
|
if (n == 0) {
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: no fork at all!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
Checkpoint port of xv6 to x86-64. Passed usertests on 2 processors a few times.
The x86-64 doesn't just add two levels to page tables to support 64 bit
addresses, but is a different processor. For example, calling conventions,
system calls, and segmentation are different from 32-bit x86. Segmentation is
basically gone, but gs/fs in combination with MSRs can be used to hold a
per-core pointer. In general, x86-64 is more straightforward than 32-bit
x86. The port uses code from sv6 and the xv6 "rsc-amd64" branch.
A summary of the changes is as follows:
- Booting: switch to grub instead of xv6's bootloader (pass -kernel to qemu),
because xv6's boot loader doesn't understand 64bit ELF files. And, we don't
care anymore about booting.
- Makefile: use -m64 instead of -m32 flag for gcc, delete boot loader, xv6.img,
bochs, and memfs. For now dont' use -O2, since usertests with -O2 is bigger than
MAXFILE!
- Update gdb.tmpl to be for i386 or x86-64
- Console/printf: use stdarg.h and treat 64-bit addresses different from ints
(32-bit)
- Update elfhdr to be 64 bit
- entry.S/entryother.S: add code to switch to 64-bit mode: build a simple page
table in 32-bit mode before switching to 64-bit mode, share code for entering
boot processor and APs, and tweak boot gdt. The boot gdt is the gdt that the
kernel proper also uses. (In 64-bit mode, the gdt/segmentation and task state
mostly disappear.)
- exec.c: fix passing argv (64-bit now instead of 32-bit).
- initcode.c: use syscall instead of int.
- kernel.ld: load kernel very high, in top terabyte. 64 bits is a lot of
address space!
- proc.c: initial return is through new syscall path instead of trapret.
- proc.h: update struct cpu to have some scratch space since syscall saves less
state than int, update struct context to reflect x86-64 calling conventions.
- swtch: simplify for x86-64 calling conventions.
- syscall: add fetcharg to handle x86-64 calling convetions (6 arguments are
passed through registers), and fetchaddr to read a 64-bit value from user space.
- sysfile: update to handle pointers from user space (e.g., sys_exec), which are
64 bits.
- trap.c: no special trap vector for sys calls, because x86-64 has a different
plan for system calls.
- trapasm: one plan for syscalls and one plan for traps (interrupt and
exceptions). On x86-64, the kernel is responsible for switching user/kernel
stacks. To do, xv6 keeps some scratch space in the cpu structure, and uses MSR
GS_KERN_BASE to point to the core's cpu structure (using swapgs).
- types.h: add uint64, and change pde_t to uint64
- usertests: exit() when fork fails, which helped in tracking down one of the
bugs in the switch from 32-bit to 64-bit
- vectors: update to make them 64 bits
- vm.c: use bootgdt in kernel too, program MSRs for syscalls and core-local
state (for swapgs), walk 4 levels in walkpgdir, add DEVSPACETOP, use task
segment to set kernel stack for interrupts (but simpler than in 32-bit mode),
add an extra argument to freevm (size of user part of address space) to avoid
checking all entries till KERNBASE (there are MANY TB before the top 1TB).
- x86: update trapframe to have 64-bit entries, which is what the processor
pushes on syscalls and traps. simplify lgdt and lidt, using struct desctr,
which needs the gcc directives packed and aligned.
TODO:
- use int32 instead of int?
- simplify curproc(). xv6 has per-cpu state again, but this time it must have it.
- avoid repetition in walkpgdir
- fix validateint() in usertests.c
- fix bugs (e.g., observed one a case of entering kernel with invalid gs or proc
2018-09-23 12:24:42 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 16:58:00 +00:00
|
|
|
if(n == N){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: fork claimed to work 1000 times!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-24 20:20:23 +00:00
|
|
|
}
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-24 20:20:23 +00:00
|
|
|
for(; n > 0; n--){
|
2019-09-10 16:30:10 +00:00
|
|
|
if(wait(0) < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: wait stopped early\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-24 20:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2019-09-10 16:30:10 +00:00
|
|
|
if(wait(0) != -1){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: wait got too many\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-24 20:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-05 16:10:54 +00:00
|
|
|
void
|
2019-09-20 21:07:41 +00:00
|
|
|
sbrkbasic(char *s)
|
2010-08-05 16:10:54 +00:00
|
|
|
{
|
2019-09-20 21:07:41 +00:00
|
|
|
enum { TOOMUCH=1024*1024*1024};
|
|
|
|
int i, pid, xstatus;
|
|
|
|
char *c, *a, *b;
|
2010-08-10 21:08:41 +00:00
|
|
|
|
2019-07-01 21:01:50 +00:00
|
|
|
// does sbrk() return the expected failure value?
|
2020-08-27 10:21:10 +00:00
|
|
|
pid = fork();
|
|
|
|
if(pid < 0){
|
|
|
|
printf("fork failed in sbrkbasic\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(pid == 0){
|
|
|
|
a = sbrk(TOOMUCH);
|
|
|
|
if(a == (char*)0xffffffffffffffffL){
|
|
|
|
// it's OK if this fails.
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(b = a; b < a+TOOMUCH; b += 4096){
|
|
|
|
*b = 99;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we should not get here! either sbrk(TOOMUCH)
|
|
|
|
// should have failed, or (with lazy allocation)
|
|
|
|
// a pagefault should have killed this process.
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
wait(&xstatus);
|
|
|
|
if(xstatus == 1){
|
|
|
|
printf("%s: too much memory allocated!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2019-07-01 21:01:50 +00:00
|
|
|
}
|
|
|
|
|
2010-08-10 21:08:41 +00:00
|
|
|
// can one sbrk() less than a page?
|
2011-01-11 18:01:13 +00:00
|
|
|
a = sbrk(0);
|
2016-08-25 13:13:00 +00:00
|
|
|
for(i = 0; i < 5000; i++){
|
2011-01-11 18:01:13 +00:00
|
|
|
b = sbrk(1);
|
2010-08-05 16:10:54 +00:00
|
|
|
if(b != a){
|
2021-01-03 13:07:12 +00:00
|
|
|
printf("%s: sbrk test failed %d %x %x\n", s, i, a, b);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2010-08-05 16:10:54 +00:00
|
|
|
}
|
|
|
|
*b = 1;
|
|
|
|
a = b + 1;
|
|
|
|
}
|
2010-08-10 21:08:41 +00:00
|
|
|
pid = fork();
|
2010-08-05 16:10:54 +00:00
|
|
|
if(pid < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: sbrk test fork failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2010-08-05 16:10:54 +00:00
|
|
|
}
|
2011-01-11 18:01:13 +00:00
|
|
|
c = sbrk(1);
|
2010-08-05 16:10:54 +00:00
|
|
|
c = sbrk(1);
|
|
|
|
if(c != a + 1){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: sbrk test failed post-fork\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2010-08-05 16:10:54 +00:00
|
|
|
}
|
|
|
|
if(pid == 0)
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2019-09-20 21:07:41 +00:00
|
|
|
wait(&xstatus);
|
|
|
|
exit(xstatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sbrkmuch(char *s)
|
|
|
|
{
|
2019-09-21 06:07:24 +00:00
|
|
|
enum { BIG=100*1024*1024 };
|
|
|
|
char *c, *oldbrk, *a, *lastaddr, *p;
|
2019-09-20 21:07:41 +00:00
|
|
|
uint64 amt;
|
|
|
|
|
|
|
|
oldbrk = sbrk(0);
|
2010-08-10 21:08:41 +00:00
|
|
|
|
2011-08-08 03:03:48 +00:00
|
|
|
// can one grow address space to something big?
|
2010-08-10 21:08:41 +00:00
|
|
|
a = sbrk(0);
|
2019-07-02 15:45:06 +00:00
|
|
|
amt = BIG - (uint64)a;
|
2011-01-11 18:01:13 +00:00
|
|
|
p = sbrk(amt);
|
2016-08-25 13:13:00 +00:00
|
|
|
if (p != a) {
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: sbrk test failed to grow big address space; enough phys mem?\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2010-08-10 21:08:41 +00:00
|
|
|
}
|
2020-08-19 17:10:14 +00:00
|
|
|
|
|
|
|
// touch each page to make sure it exists.
|
|
|
|
char *eee = sbrk(0);
|
|
|
|
for(char *pp = a; pp < eee; pp += 4096)
|
|
|
|
*pp = 1;
|
|
|
|
|
2011-08-08 03:03:48 +00:00
|
|
|
lastaddr = (char*) (BIG-1);
|
2010-08-10 21:08:41 +00:00
|
|
|
*lastaddr = 99;
|
|
|
|
|
|
|
|
// can one de-allocate?
|
|
|
|
a = sbrk(0);
|
2019-08-20 16:58:00 +00:00
|
|
|
c = sbrk(-PGSIZE);
|
2019-07-01 21:01:50 +00:00
|
|
|
if(c == (char*)0xffffffffffffffffL){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: sbrk could not deallocate\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2010-08-10 21:08:41 +00:00
|
|
|
}
|
|
|
|
c = sbrk(0);
|
2019-08-20 16:58:00 +00:00
|
|
|
if(c != a - PGSIZE){
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("%s: sbrk deallocation produced wrong address, a %x c %x\n", s, a, c);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2010-08-10 21:08:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// can one re-allocate that page?
|
|
|
|
a = sbrk(0);
|
2019-08-20 16:58:00 +00:00
|
|
|
c = sbrk(PGSIZE);
|
|
|
|
if(c != a || sbrk(0) != a + PGSIZE){
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("%s: sbrk re-allocation failed, a %x c %x\n", s, a, c);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2010-08-10 21:08:41 +00:00
|
|
|
}
|
|
|
|
if(*lastaddr == 99){
|
|
|
|
// should be zero
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: sbrk de-allocation didn't really deallocate\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2010-08-10 21:08:41 +00:00
|
|
|
}
|
|
|
|
|
2011-08-08 13:20:29 +00:00
|
|
|
a = sbrk(0);
|
|
|
|
c = sbrk(-(sbrk(0) - oldbrk));
|
|
|
|
if(c != a){
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("%s: sbrk downsize failed, a %x c %x\n", s, a, c);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2011-08-08 13:20:29 +00:00
|
|
|
}
|
2019-09-21 06:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// can we read the kernel's memory?
|
|
|
|
void
|
|
|
|
kernmem(char *s)
|
|
|
|
{
|
|
|
|
char *a;
|
|
|
|
int pid;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2011-08-08 03:03:48 +00:00
|
|
|
for(a = (char*)(KERNBASE); a < (char*) (KERNBASE+2000000); a += 50000){
|
2011-01-11 18:01:13 +00:00
|
|
|
pid = fork();
|
2010-08-11 18:34:45 +00:00
|
|
|
if(pid < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: fork failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2010-08-11 18:34:45 +00:00
|
|
|
}
|
|
|
|
if(pid == 0){
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("%s: oops could read %x = %x\n", s, a, *a);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2010-08-11 18:34:45 +00:00
|
|
|
}
|
2019-09-21 06:07:24 +00:00
|
|
|
int xstatus;
|
|
|
|
wait(&xstatus);
|
|
|
|
if(xstatus != -1) // did kernel kill child?
|
|
|
|
exit(1);
|
2010-08-11 18:34:45 +00:00
|
|
|
}
|
2019-09-21 06:07:24 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 15:06:24 +00:00
|
|
|
// user code should not be able to write to addresses above MAXVA.
|
|
|
|
void
|
|
|
|
MAXVAplus(char *s)
|
|
|
|
{
|
|
|
|
volatile uint64 a = MAXVA;
|
|
|
|
for( ; a != 0; a <<= 1){
|
|
|
|
int pid;
|
|
|
|
pid = fork();
|
|
|
|
if(pid < 0){
|
|
|
|
printf("%s: fork failed\n", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(pid == 0){
|
|
|
|
*(char*)a = 99;
|
|
|
|
printf("%s: oops wrote %x\n", s, a);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
int xstatus;
|
|
|
|
wait(&xstatus);
|
|
|
|
if(xstatus != -1) // did kernel kill child?
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-21 06:07:24 +00:00
|
|
|
// if we run the system out of memory, does it clean up the last
|
|
|
|
// failed allocation?
|
|
|
|
void
|
|
|
|
sbrkfail(char *s)
|
|
|
|
{
|
|
|
|
enum { BIG=100*1024*1024 };
|
|
|
|
int i, xstatus;
|
|
|
|
int fds[2];
|
|
|
|
char scratch;
|
|
|
|
char *c, *a;
|
|
|
|
int pids[10];
|
|
|
|
int pid;
|
|
|
|
|
2010-09-01 01:49:49 +00:00
|
|
|
if(pipe(fds) != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: pipe() failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2010-09-01 01:49:49 +00:00
|
|
|
}
|
2010-09-01 04:41:25 +00:00
|
|
|
for(i = 0; i < sizeof(pids)/sizeof(pids[0]); i++){
|
2011-01-11 18:01:13 +00:00
|
|
|
if((pids[i] = fork()) == 0){
|
2011-08-08 13:20:29 +00:00
|
|
|
// allocate a lot of memory
|
2020-08-27 10:21:10 +00:00
|
|
|
sbrk(BIG - (uint64)sbrk(0));
|
2010-09-01 01:49:49 +00:00
|
|
|
write(fds[1], "x", 1);
|
|
|
|
// sit around until killed
|
2010-09-01 04:41:25 +00:00
|
|
|
for(;;) sleep(1000);
|
2010-09-01 01:49:49 +00:00
|
|
|
}
|
2010-09-01 04:41:25 +00:00
|
|
|
if(pids[i] != -1)
|
2010-09-01 01:49:49 +00:00
|
|
|
read(fds[0], &scratch, 1);
|
|
|
|
}
|
Checkpoint port of xv6 to x86-64. Passed usertests on 2 processors a few times.
The x86-64 doesn't just add two levels to page tables to support 64 bit
addresses, but is a different processor. For example, calling conventions,
system calls, and segmentation are different from 32-bit x86. Segmentation is
basically gone, but gs/fs in combination with MSRs can be used to hold a
per-core pointer. In general, x86-64 is more straightforward than 32-bit
x86. The port uses code from sv6 and the xv6 "rsc-amd64" branch.
A summary of the changes is as follows:
- Booting: switch to grub instead of xv6's bootloader (pass -kernel to qemu),
because xv6's boot loader doesn't understand 64bit ELF files. And, we don't
care anymore about booting.
- Makefile: use -m64 instead of -m32 flag for gcc, delete boot loader, xv6.img,
bochs, and memfs. For now dont' use -O2, since usertests with -O2 is bigger than
MAXFILE!
- Update gdb.tmpl to be for i386 or x86-64
- Console/printf: use stdarg.h and treat 64-bit addresses different from ints
(32-bit)
- Update elfhdr to be 64 bit
- entry.S/entryother.S: add code to switch to 64-bit mode: build a simple page
table in 32-bit mode before switching to 64-bit mode, share code for entering
boot processor and APs, and tweak boot gdt. The boot gdt is the gdt that the
kernel proper also uses. (In 64-bit mode, the gdt/segmentation and task state
mostly disappear.)
- exec.c: fix passing argv (64-bit now instead of 32-bit).
- initcode.c: use syscall instead of int.
- kernel.ld: load kernel very high, in top terabyte. 64 bits is a lot of
address space!
- proc.c: initial return is through new syscall path instead of trapret.
- proc.h: update struct cpu to have some scratch space since syscall saves less
state than int, update struct context to reflect x86-64 calling conventions.
- swtch: simplify for x86-64 calling conventions.
- syscall: add fetcharg to handle x86-64 calling convetions (6 arguments are
passed through registers), and fetchaddr to read a 64-bit value from user space.
- sysfile: update to handle pointers from user space (e.g., sys_exec), which are
64 bits.
- trap.c: no special trap vector for sys calls, because x86-64 has a different
plan for system calls.
- trapasm: one plan for syscalls and one plan for traps (interrupt and
exceptions). On x86-64, the kernel is responsible for switching user/kernel
stacks. To do, xv6 keeps some scratch space in the cpu structure, and uses MSR
GS_KERN_BASE to point to the core's cpu structure (using swapgs).
- types.h: add uint64, and change pde_t to uint64
- usertests: exit() when fork fails, which helped in tracking down one of the
bugs in the switch from 32-bit to 64-bit
- vectors: update to make them 64 bits
- vm.c: use bootgdt in kernel too, program MSRs for syscalls and core-local
state (for swapgs), walk 4 levels in walkpgdir, add DEVSPACETOP, use task
segment to set kernel stack for interrupts (but simpler than in 32-bit mode),
add an extra argument to freevm (size of user part of address space) to avoid
checking all entries till KERNBASE (there are MANY TB before the top 1TB).
- x86: update trapframe to have 64-bit entries, which is what the processor
pushes on syscalls and traps. simplify lgdt and lidt, using struct desctr,
which needs the gcc directives packed and aligned.
TODO:
- use int32 instead of int?
- simplify curproc(). xv6 has per-cpu state again, but this time it must have it.
- avoid repetition in walkpgdir
- fix validateint() in usertests.c
- fix bugs (e.g., observed one a case of entering kernel with invalid gs or proc
2018-09-23 12:24:42 +00:00
|
|
|
|
2010-09-01 01:49:49 +00:00
|
|
|
// if those failed allocations freed up the pages they did allocate,
|
|
|
|
// we'll be able to allocate here
|
2019-08-20 16:58:00 +00:00
|
|
|
c = sbrk(PGSIZE);
|
2010-09-01 04:41:25 +00:00
|
|
|
for(i = 0; i < sizeof(pids)/sizeof(pids[0]); i++){
|
|
|
|
if(pids[i] == -1)
|
2010-09-01 01:49:49 +00:00
|
|
|
continue;
|
|
|
|
kill(pids[i]);
|
2019-09-10 16:30:10 +00:00
|
|
|
wait(0);
|
2010-09-01 01:49:49 +00:00
|
|
|
}
|
2019-07-01 21:01:50 +00:00
|
|
|
if(c == (char*)0xffffffffffffffffL){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: failed sbrk leaked memory\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2010-09-01 01:49:49 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 12:37:26 +00:00
|
|
|
// test running fork with the above allocated page
|
|
|
|
pid = fork();
|
|
|
|
if(pid < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: fork failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2019-07-24 12:37:26 +00:00
|
|
|
}
|
|
|
|
if(pid == 0){
|
2020-08-27 10:21:10 +00:00
|
|
|
// allocate a lot of memory.
|
|
|
|
// this should produce a page fault,
|
|
|
|
// and thus not complete.
|
2019-07-24 12:37:26 +00:00
|
|
|
a = sbrk(0);
|
|
|
|
sbrk(10*BIG);
|
|
|
|
int n = 0;
|
2019-08-20 16:58:00 +00:00
|
|
|
for (i = 0; i < 10*BIG; i += PGSIZE) {
|
2019-07-24 12:37:26 +00:00
|
|
|
n += *(a+i);
|
|
|
|
}
|
2020-08-27 10:21:10 +00:00
|
|
|
// print n so the compiler doesn't optimize away
|
|
|
|
// the for loop.
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("%s: allocate a lot of memory succeeded %d\n", s, n);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2019-07-24 12:37:26 +00:00
|
|
|
}
|
2019-09-21 06:07:24 +00:00
|
|
|
wait(&xstatus);
|
2020-08-27 10:21:10 +00:00
|
|
|
if(xstatus != -1 && xstatus != 2)
|
2019-09-21 06:07:24 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// test reads/writes from/to allocated memory
|
|
|
|
void
|
|
|
|
sbrkarg(char *s)
|
|
|
|
{
|
|
|
|
char *a;
|
|
|
|
int fd, n;
|
2019-07-24 12:37:26 +00:00
|
|
|
|
2019-08-20 16:58:00 +00:00
|
|
|
a = sbrk(PGSIZE);
|
2019-07-24 12:37:26 +00:00
|
|
|
fd = open("sbrk", O_CREATE|O_WRONLY);
|
|
|
|
unlink("sbrk");
|
|
|
|
if(fd < 0) {
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: open sbrk failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2019-07-24 12:37:26 +00:00
|
|
|
}
|
2019-09-21 18:20:21 +00:00
|
|
|
if ((n = write(fd, a, PGSIZE)) < 0) {
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: write sbrk failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2019-07-24 12:37:26 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
// test writes to allocated memory
|
2019-08-20 16:58:00 +00:00
|
|
|
a = sbrk(PGSIZE);
|
2019-07-24 12:37:26 +00:00
|
|
|
if(pipe((int *) a) != 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: pipe() failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2019-07-24 12:37:26 +00:00
|
|
|
}
|
2010-08-05 16:10:54 +00:00
|
|
|
}
|
|
|
|
|
2010-09-01 20:46:37 +00:00
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
validatetest(char *s)
|
2010-09-01 20:46:37 +00:00
|
|
|
{
|
2019-08-20 17:04:12 +00:00
|
|
|
int hi;
|
Checkpoint port of xv6 to x86-64. Passed usertests on 2 processors a few times.
The x86-64 doesn't just add two levels to page tables to support 64 bit
addresses, but is a different processor. For example, calling conventions,
system calls, and segmentation are different from 32-bit x86. Segmentation is
basically gone, but gs/fs in combination with MSRs can be used to hold a
per-core pointer. In general, x86-64 is more straightforward than 32-bit
x86. The port uses code from sv6 and the xv6 "rsc-amd64" branch.
A summary of the changes is as follows:
- Booting: switch to grub instead of xv6's bootloader (pass -kernel to qemu),
because xv6's boot loader doesn't understand 64bit ELF files. And, we don't
care anymore about booting.
- Makefile: use -m64 instead of -m32 flag for gcc, delete boot loader, xv6.img,
bochs, and memfs. For now dont' use -O2, since usertests with -O2 is bigger than
MAXFILE!
- Update gdb.tmpl to be for i386 or x86-64
- Console/printf: use stdarg.h and treat 64-bit addresses different from ints
(32-bit)
- Update elfhdr to be 64 bit
- entry.S/entryother.S: add code to switch to 64-bit mode: build a simple page
table in 32-bit mode before switching to 64-bit mode, share code for entering
boot processor and APs, and tweak boot gdt. The boot gdt is the gdt that the
kernel proper also uses. (In 64-bit mode, the gdt/segmentation and task state
mostly disappear.)
- exec.c: fix passing argv (64-bit now instead of 32-bit).
- initcode.c: use syscall instead of int.
- kernel.ld: load kernel very high, in top terabyte. 64 bits is a lot of
address space!
- proc.c: initial return is through new syscall path instead of trapret.
- proc.h: update struct cpu to have some scratch space since syscall saves less
state than int, update struct context to reflect x86-64 calling conventions.
- swtch: simplify for x86-64 calling conventions.
- syscall: add fetcharg to handle x86-64 calling convetions (6 arguments are
passed through registers), and fetchaddr to read a 64-bit value from user space.
- sysfile: update to handle pointers from user space (e.g., sys_exec), which are
64 bits.
- trap.c: no special trap vector for sys calls, because x86-64 has a different
plan for system calls.
- trapasm: one plan for syscalls and one plan for traps (interrupt and
exceptions). On x86-64, the kernel is responsible for switching user/kernel
stacks. To do, xv6 keeps some scratch space in the cpu structure, and uses MSR
GS_KERN_BASE to point to the core's cpu structure (using swapgs).
- types.h: add uint64, and change pde_t to uint64
- usertests: exit() when fork fails, which helped in tracking down one of the
bugs in the switch from 32-bit to 64-bit
- vectors: update to make them 64 bits
- vm.c: use bootgdt in kernel too, program MSRs for syscalls and core-local
state (for swapgs), walk 4 levels in walkpgdir, add DEVSPACETOP, use task
segment to set kernel stack for interrupts (but simpler than in 32-bit mode),
add an extra argument to freevm (size of user part of address space) to avoid
checking all entries till KERNBASE (there are MANY TB before the top 1TB).
- x86: update trapframe to have 64-bit entries, which is what the processor
pushes on syscalls and traps. simplify lgdt and lidt, using struct desctr,
which needs the gcc directives packed and aligned.
TODO:
- use int32 instead of int?
- simplify curproc(). xv6 has per-cpu state again, but this time it must have it.
- avoid repetition in walkpgdir
- fix validateint() in usertests.c
- fix bugs (e.g., observed one a case of entering kernel with invalid gs or proc
2018-09-23 12:24:42 +00:00
|
|
|
uint64 p;
|
2010-09-01 20:46:37 +00:00
|
|
|
|
2011-01-11 18:01:13 +00:00
|
|
|
hi = 1100*1024;
|
2019-08-20 16:58:00 +00:00
|
|
|
for(p = 0; p <= (uint)hi; p += PGSIZE){
|
2010-09-01 20:46:37 +00:00
|
|
|
// try to crash the kernel by passing in a bad string pointer
|
2011-01-11 18:01:13 +00:00
|
|
|
if(link("nosuchfile", (char*)p) != -1){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: link should not succeed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2010-09-01 20:46:37 +00:00
|
|
|
}
|
2010-08-06 15:12:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 03:29:42 +00:00
|
|
|
// does uninitialized data start out zero?
|
2010-09-19 17:47:52 +00:00
|
|
|
char uninit[10000];
|
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
bsstest(char *s)
|
2010-09-19 17:47:52 +00:00
|
|
|
{
|
|
|
|
int i;
|
2011-01-11 18:01:13 +00:00
|
|
|
|
2010-09-19 17:47:52 +00:00
|
|
|
for(i = 0; i < sizeof(uninit); i++){
|
|
|
|
if(uninit[i] != '\0'){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: bss test failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2010-09-19 17:47:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-01 16:02:49 +00:00
|
|
|
// does exec return an error if the arguments
|
|
|
|
// are larger than a page? or does it write
|
|
|
|
// below the stack and wreck the instructions/data?
|
2010-09-20 10:00:22 +00:00
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
bigargtest(char *s)
|
2010-09-20 10:00:22 +00:00
|
|
|
{
|
2019-09-19 10:58:28 +00:00
|
|
|
int pid, fd, xstatus;
|
2010-09-20 10:00:22 +00:00
|
|
|
|
2011-09-01 16:02:49 +00:00
|
|
|
unlink("bigarg-ok");
|
2010-09-20 10:00:22 +00:00
|
|
|
pid = fork();
|
|
|
|
if(pid == 0){
|
2011-09-01 16:02:49 +00:00
|
|
|
static char *args[MAXARG];
|
2010-09-20 10:00:22 +00:00
|
|
|
int i;
|
2011-09-01 16:02:49 +00:00
|
|
|
for(i = 0; i < MAXARG-1; i++)
|
|
|
|
args[i] = "bigargs test: failed\n ";
|
|
|
|
args[MAXARG-1] = 0;
|
2010-09-20 10:00:22 +00:00
|
|
|
exec("echo", args);
|
2011-09-01 16:02:49 +00:00
|
|
|
fd = open("bigarg-ok", O_CREATE);
|
|
|
|
close(fd);
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2010-09-20 10:00:22 +00:00
|
|
|
} else if(pid < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: bigargtest: fork failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2010-09-20 10:00:22 +00:00
|
|
|
}
|
2019-09-19 10:58:28 +00:00
|
|
|
|
|
|
|
wait(&xstatus);
|
|
|
|
if(xstatus != 0)
|
|
|
|
exit(xstatus);
|
2011-09-01 16:02:49 +00:00
|
|
|
fd = open("bigarg-ok", 0);
|
|
|
|
if(fd < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: bigarg test failed!\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2011-09-01 16:02:49 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
2010-09-20 10:00:22 +00:00
|
|
|
}
|
|
|
|
|
2011-08-19 17:30:57 +00:00
|
|
|
// what happens when the file system runs out of blocks?
|
|
|
|
// answer: balloc panics, so this test is not useful.
|
|
|
|
void
|
|
|
|
fsfull()
|
|
|
|
{
|
|
|
|
int nfiles;
|
|
|
|
int fsblocks = 0;
|
|
|
|
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("fsfull test\n");
|
2011-08-19 17:30:57 +00:00
|
|
|
|
|
|
|
for(nfiles = 0; ; nfiles++){
|
|
|
|
char name[64];
|
|
|
|
name[0] = 'f';
|
|
|
|
name[1] = '0' + nfiles / 1000;
|
|
|
|
name[2] = '0' + (nfiles % 1000) / 100;
|
|
|
|
name[3] = '0' + (nfiles % 100) / 10;
|
|
|
|
name[4] = '0' + (nfiles % 10);
|
|
|
|
name[5] = '\0';
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("writing %s\n", name);
|
2011-08-19 17:30:57 +00:00
|
|
|
int fd = open(name, O_CREATE|O_RDWR);
|
|
|
|
if(fd < 0){
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("open %s failed\n", name);
|
2011-08-19 17:30:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
int total = 0;
|
|
|
|
while(1){
|
2019-08-20 16:58:00 +00:00
|
|
|
int cc = write(fd, buf, BSIZE);
|
|
|
|
if(cc < BSIZE)
|
2011-08-19 17:30:57 +00:00
|
|
|
break;
|
|
|
|
total += cc;
|
|
|
|
fsblocks++;
|
|
|
|
}
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("wrote %d bytes\n", total);
|
2011-08-19 17:30:57 +00:00
|
|
|
close(fd);
|
|
|
|
if(total == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(nfiles >= 0){
|
|
|
|
char name[64];
|
|
|
|
name[0] = 'f';
|
|
|
|
name[1] = '0' + nfiles / 1000;
|
|
|
|
name[2] = '0' + (nfiles % 1000) / 100;
|
|
|
|
name[3] = '0' + (nfiles % 100) / 10;
|
|
|
|
name[4] = '0' + (nfiles % 10);
|
|
|
|
name[5] = '\0';
|
|
|
|
unlink(name);
|
|
|
|
nfiles--;
|
|
|
|
}
|
|
|
|
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("fsfull test finished\n");
|
2011-08-19 17:30:57 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 10:58:28 +00:00
|
|
|
void argptest(char *s)
|
2016-09-26 11:54:02 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
fd = open("init", O_RDONLY);
|
|
|
|
if (fd < 0) {
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: open failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2016-09-26 11:54:02 +00:00
|
|
|
}
|
|
|
|
read(fd, sbrk(0) - 1, -1);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2019-07-23 16:17:17 +00:00
|
|
|
// check that there's an invalid page beneath
|
|
|
|
// the user stack, to catch stack overflow.
|
2019-07-22 22:08:52 +00:00
|
|
|
void
|
2019-09-19 10:58:28 +00:00
|
|
|
stacktest(char *s)
|
2019-07-22 22:08:52 +00:00
|
|
|
{
|
|
|
|
int pid;
|
2019-09-19 10:58:28 +00:00
|
|
|
int xstatus;
|
2019-07-22 22:08:52 +00:00
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if(pid == 0) {
|
|
|
|
char *sp = (char *) r_sp();
|
2019-08-20 16:58:00 +00:00
|
|
|
sp -= PGSIZE;
|
2019-07-23 16:17:17 +00:00
|
|
|
// the *sp should cause a trap.
|
2020-10-02 11:51:15 +00:00
|
|
|
printf("%s: stacktest: read below stack %p\n", s, *sp);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2019-07-22 22:08:52 +00:00
|
|
|
} else if(pid < 0){
|
2019-09-19 10:58:28 +00:00
|
|
|
printf("%s: fork failed\n", s);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2019-07-22 22:08:52 +00:00
|
|
|
}
|
2019-09-19 10:58:28 +00:00
|
|
|
wait(&xstatus);
|
|
|
|
if(xstatus == -1) // kernel killed child?
|
|
|
|
exit(0);
|
|
|
|
else
|
|
|
|
exit(xstatus);
|
|
|
|
}
|
|
|
|
|
2022-08-15 23:02:19 +00:00
|
|
|
// check that writes to text segment fault
|
|
|
|
void
|
2022-08-23 15:21:26 +00:00
|
|
|
textwrite(char *s)
|
2022-08-15 23:02:19 +00:00
|
|
|
{
|
|
|
|
int pid;
|
|
|
|
int xstatus;
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if(pid == 0) {
|
|
|
|
volatile int *addr = (int *) 0;
|
|
|
|
*addr = 10;
|
|
|
|
exit(1);
|
|
|
|
} else if(pid < 0){
|
|
|
|
printf("%s: fork failed\n", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
wait(&xstatus);
|
|
|
|
if(xstatus == -1) // kernel killed child?
|
|
|
|
exit(0);
|
|
|
|
else
|
|
|
|
exit(xstatus);
|
|
|
|
}
|
|
|
|
|
2019-09-23 10:50:25 +00:00
|
|
|
// regression test. copyin(), copyout(), and copyinstr() used to cast
|
|
|
|
// the virtual page address to uint, which (with certain wild system
|
|
|
|
// call arguments) resulted in a kernel page faults.
|
2022-08-15 23:02:19 +00:00
|
|
|
void *big = (void*) 0xeaeb0b5b00002f5e;
|
2019-09-20 13:41:03 +00:00
|
|
|
void
|
|
|
|
pgbug(char *s)
|
|
|
|
{
|
|
|
|
char *argv[1];
|
|
|
|
argv[0] = 0;
|
2022-08-15 23:02:19 +00:00
|
|
|
exec(big, argv);
|
|
|
|
pipe(big);
|
2019-09-20 14:27:03 +00:00
|
|
|
|
2019-09-20 13:41:03 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2019-09-23 10:50:25 +00:00
|
|
|
// regression test. does the kernel panic if a process sbrk()s its
|
|
|
|
// size to be less than a page, or zero, or reduces the break by an
|
|
|
|
// amount too small to cause a page to be freed?
|
2019-09-20 15:35:27 +00:00
|
|
|
void
|
2019-09-20 16:13:57 +00:00
|
|
|
sbrkbugs(char *s)
|
2019-09-20 15:35:27 +00:00
|
|
|
{
|
|
|
|
int pid = fork();
|
|
|
|
if(pid < 0){
|
|
|
|
printf("fork failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(pid == 0){
|
|
|
|
int sz = (uint64) sbrk(0);
|
|
|
|
// free all user memory; there used to be a bug that
|
|
|
|
// would not adjust p->sz correctly in this case,
|
|
|
|
// causing exit() to panic.
|
|
|
|
sbrk(-sz);
|
|
|
|
// user page fault here.
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
wait(0);
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if(pid < 0){
|
|
|
|
printf("fork failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(pid == 0){
|
|
|
|
int sz = (uint64) sbrk(0);
|
|
|
|
// set the break to somewhere in the very first
|
|
|
|
// page; there used to be a bug that would incorrectly
|
|
|
|
// free the first page.
|
|
|
|
sbrk(-(sz - 3500));
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
wait(0);
|
|
|
|
|
2019-09-20 16:13:57 +00:00
|
|
|
pid = fork();
|
|
|
|
if(pid < 0){
|
|
|
|
printf("fork failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(pid == 0){
|
|
|
|
// set the break in the middle of a page.
|
|
|
|
sbrk((10*4096 + 2048) - (uint64)sbrk(0));
|
|
|
|
|
|
|
|
// reduce the break a bit, but not enough to
|
|
|
|
// cause a page to be freed. this used to cause
|
|
|
|
// a panic.
|
|
|
|
sbrk(-10);
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
wait(0);
|
|
|
|
|
2019-09-20 15:35:27 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2021-07-02 13:57:14 +00:00
|
|
|
// if process size was somewhat more than a page boundary, and then
|
|
|
|
// shrunk to be somewhat less than that page boundary, can the kernel
|
|
|
|
// still copyin() from addresses in the last page?
|
|
|
|
void
|
|
|
|
sbrklast(char *s)
|
|
|
|
{
|
|
|
|
uint64 top = (uint64) sbrk(0);
|
|
|
|
if((top % 4096) != 0)
|
|
|
|
sbrk(4096 - (top % 4096));
|
|
|
|
sbrk(4096);
|
|
|
|
sbrk(10);
|
|
|
|
sbrk(-20);
|
|
|
|
top = (uint64) sbrk(0);
|
|
|
|
char *p = (char *) (top - 64);
|
|
|
|
p[0] = 'x';
|
|
|
|
p[1] = '\0';
|
|
|
|
int fd = open(p, O_RDWR|O_CREATE);
|
|
|
|
write(fd, p, 1);
|
|
|
|
close(fd);
|
|
|
|
fd = open(p, O_RDWR);
|
|
|
|
p[0] = '\0';
|
|
|
|
read(fd, p, 1);
|
|
|
|
if(p[0] != 'x')
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-08-15 23:02:19 +00:00
|
|
|
|
2021-07-02 18:24:45 +00:00
|
|
|
// does sbrk handle signed int32 wrap-around with
|
|
|
|
// negative arguments?
|
|
|
|
void
|
|
|
|
sbrk8000(char *s)
|
|
|
|
{
|
|
|
|
sbrk(0x80000004);
|
|
|
|
volatile char *top = sbrk(0);
|
|
|
|
*(top-1) = *(top-1) + 1;
|
|
|
|
}
|
|
|
|
|
2022-08-15 23:02:19 +00:00
|
|
|
|
2022-08-25 13:45:35 +00:00
|
|
|
|
|
|
|
// regression test. test whether exec() leaks memory if one of the
|
|
|
|
// arguments is invalid. the test passes if the kernel doesn't panic.
|
|
|
|
void
|
|
|
|
badarg(char *s)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < 50000; i++){
|
|
|
|
char *argv[2];
|
|
|
|
argv[0] = (char*)0xffffffff;
|
|
|
|
argv[1] = 0;
|
|
|
|
exec("echo", argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct test {
|
|
|
|
void (*f)(char *);
|
|
|
|
char *s;
|
|
|
|
} quicktests[] = {
|
|
|
|
{copyin, "copyin"},
|
|
|
|
{copyout, "copyout"},
|
|
|
|
{copyinstr1, "copyinstr1"},
|
|
|
|
{copyinstr2, "copyinstr2"},
|
|
|
|
{copyinstr3, "copyinstr3"},
|
|
|
|
{rwsbrk, "rwsbrk" },
|
|
|
|
{truncate1, "truncate1"},
|
|
|
|
{truncate2, "truncate2"},
|
|
|
|
{truncate3, "truncate3"},
|
|
|
|
{openiputtest, "openiput"},
|
|
|
|
{exitiputtest, "exitiput"},
|
|
|
|
{iputtest, "iput"},
|
|
|
|
{opentest, "opentest"},
|
|
|
|
{writetest, "writetest"},
|
|
|
|
{writebig, "writebig"},
|
|
|
|
{createtest, "createtest"},
|
|
|
|
{dirtest, "dirtest"},
|
|
|
|
{exectest, "exectest"},
|
|
|
|
{pipe1, "pipe1"},
|
|
|
|
{killstatus, "killstatus"},
|
|
|
|
{preempt, "preempt"},
|
|
|
|
{exitwait, "exitwait"},
|
|
|
|
{reparent, "reparent" },
|
|
|
|
{twochildren, "twochildren"},
|
|
|
|
{forkfork, "forkfork"},
|
|
|
|
{forkforkfork, "forkforkfork"},
|
|
|
|
{reparent2, "reparent2"},
|
|
|
|
{mem, "mem"},
|
|
|
|
{sharedfd, "sharedfd"},
|
|
|
|
{fourfiles, "fourfiles"},
|
|
|
|
{createdelete, "createdelete"},
|
|
|
|
{unlinkread, "unlinkread"},
|
|
|
|
{linktest, "linktest"},
|
|
|
|
{concreate, "concreate"},
|
|
|
|
{linkunlink, "linkunlink"},
|
|
|
|
{subdir, "subdir"},
|
|
|
|
{bigwrite, "bigwrite"},
|
|
|
|
{bigfile, "bigfile"},
|
|
|
|
{fourteen, "fourteen"},
|
|
|
|
{rmdot, "rmdot"},
|
|
|
|
{dirfile, "dirfile"},
|
|
|
|
{iref, "iref"},
|
|
|
|
{forktest, "forktest"},
|
|
|
|
{sbrkbasic, "sbrkbasic"},
|
|
|
|
{sbrkmuch, "sbrkmuch"},
|
|
|
|
{kernmem, "kernmem"},
|
|
|
|
{MAXVAplus, "MAXVAplus"},
|
|
|
|
{sbrkfail, "sbrkfail"},
|
|
|
|
{sbrkarg, "sbrkarg"},
|
|
|
|
{validatetest, "validatetest"},
|
|
|
|
{bsstest, "bsstest"},
|
|
|
|
{bigargtest, "bigargtest"},
|
|
|
|
{argptest, "argptest"},
|
|
|
|
{stacktest, "stacktest"},
|
|
|
|
{textwrite, "textwrite"},
|
|
|
|
{pgbug, "pgbug" },
|
|
|
|
{sbrkbugs, "sbrkbugs" },
|
|
|
|
{sbrklast, "sbrklast"},
|
|
|
|
{sbrk8000, "sbrk8000"},
|
|
|
|
{badarg, "badarg" },
|
|
|
|
|
|
|
|
{ 0, 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Section with tests that take a fair bit of time
|
|
|
|
//
|
|
|
|
|
|
|
|
// directory that uses indirect blocks
|
|
|
|
void
|
|
|
|
bigdir(char *s)
|
|
|
|
{
|
|
|
|
enum { N = 500 };
|
|
|
|
int i, fd;
|
|
|
|
char name[10];
|
|
|
|
|
|
|
|
unlink("bd");
|
|
|
|
|
|
|
|
fd = open("bd", O_CREATE);
|
|
|
|
if(fd < 0){
|
|
|
|
printf("%s: bigdir create failed\n", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
for(i = 0; i < N; i++){
|
|
|
|
name[0] = 'x';
|
|
|
|
name[1] = '0' + (i / 64);
|
|
|
|
name[2] = '0' + (i % 64);
|
|
|
|
name[3] = '\0';
|
|
|
|
if(link("bd", name) != 0){
|
|
|
|
printf("%s: bigdir link(bd, %s) failed\n", s, name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink("bd");
|
|
|
|
for(i = 0; i < N; i++){
|
|
|
|
name[0] = 'x';
|
|
|
|
name[1] = '0' + (i / 64);
|
|
|
|
name[2] = '0' + (i % 64);
|
|
|
|
name[3] = '\0';
|
|
|
|
if(unlink(name) != 0){
|
|
|
|
printf("%s: bigdir unlink failed", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// concurrent writes to try to provoke deadlock in the virtio disk
|
|
|
|
// driver.
|
|
|
|
void
|
|
|
|
manywrites(char *s)
|
|
|
|
{
|
|
|
|
int nchildren = 4;
|
|
|
|
int howmany = 30; // increase to look for deadlock
|
|
|
|
|
|
|
|
for(int ci = 0; ci < nchildren; ci++){
|
|
|
|
int pid = fork();
|
|
|
|
if(pid < 0){
|
|
|
|
printf("fork failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pid == 0){
|
|
|
|
char name[3];
|
|
|
|
name[0] = 'b';
|
|
|
|
name[1] = 'a' + ci;
|
|
|
|
name[2] = '\0';
|
|
|
|
unlink(name);
|
|
|
|
|
|
|
|
for(int iters = 0; iters < howmany; iters++){
|
|
|
|
for(int i = 0; i < ci+1; i++){
|
|
|
|
int fd = open(name, O_CREATE | O_RDWR);
|
|
|
|
if(fd < 0){
|
|
|
|
printf("%s: cannot create %s\n", s, name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
int sz = sizeof(buf);
|
|
|
|
int cc = write(fd, buf, sz);
|
|
|
|
if(cc != sz){
|
|
|
|
printf("%s: write(%d) ret %d\n", s, sz, cc);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
unlink(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink(name);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int ci = 0; ci < nchildren; ci++){
|
|
|
|
int st = 0;
|
|
|
|
wait(&st);
|
|
|
|
if(st != 0)
|
|
|
|
exit(st);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2019-09-23 10:50:25 +00:00
|
|
|
// regression test. does write() with an invalid buffer pointer cause
|
|
|
|
// a block to be allocated for a file that is then not freed when the
|
|
|
|
// file is deleted? if the kernel has this bug, it will panic: balloc:
|
|
|
|
// out of blocks. assumed_free may need to be raised to be more than
|
|
|
|
// the number of free blocks. this test takes a long time.
|
2019-09-20 17:09:26 +00:00
|
|
|
void
|
|
|
|
badwrite(char *s)
|
|
|
|
{
|
|
|
|
int assumed_free = 600;
|
|
|
|
|
|
|
|
unlink("junk");
|
|
|
|
for(int i = 0; i < assumed_free; i++){
|
|
|
|
int fd = open("junk", O_CREATE|O_WRONLY);
|
|
|
|
if(fd < 0){
|
|
|
|
printf("open junk failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
write(fd, (char*)0xffffffffffL, 1);
|
|
|
|
close(fd);
|
|
|
|
unlink("junk");
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = open("junk", O_CREATE|O_WRONLY);
|
|
|
|
if(fd < 0){
|
|
|
|
printf("open junk failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(write(fd, "x", 1) != 1){
|
|
|
|
printf("write failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
unlink("junk");
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2020-08-19 16:35:14 +00:00
|
|
|
// test the exec() code that cleans up if it runs out
|
|
|
|
// of memory. it's really a test that such a condition
|
|
|
|
// doesn't cause a panic.
|
|
|
|
void
|
|
|
|
execout(char *s)
|
|
|
|
{
|
|
|
|
for(int avail = 0; avail < 15; avail++){
|
|
|
|
int pid = fork();
|
|
|
|
if(pid < 0){
|
|
|
|
printf("fork failed\n");
|
|
|
|
exit(1);
|
|
|
|
} else if(pid == 0){
|
|
|
|
// allocate all of memory.
|
|
|
|
while(1){
|
|
|
|
uint64 a = (uint64) sbrk(4096);
|
|
|
|
if(a == 0xffffffffffffffffLL)
|
|
|
|
break;
|
2020-08-19 17:10:14 +00:00
|
|
|
*(char*)(a + 4096 - 1) = 1;
|
2020-08-19 16:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// free a few pages, in order to let exec() make some
|
|
|
|
// progress.
|
|
|
|
for(int i = 0; i < avail; i++)
|
|
|
|
sbrk(-4096);
|
|
|
|
|
|
|
|
close(1);
|
|
|
|
char *args[] = { "echo", "x", 0 };
|
|
|
|
exec("echo", args);
|
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
wait((int*)0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2022-08-10 10:13:52 +00:00
|
|
|
// can the kernel tolerate running out of disk space?
|
|
|
|
void
|
|
|
|
diskfull(char *s)
|
|
|
|
{
|
|
|
|
int fi;
|
|
|
|
int done = 0;
|
2022-08-23 12:52:15 +00:00
|
|
|
|
|
|
|
unlink("diskfulldir");
|
2022-08-10 10:13:52 +00:00
|
|
|
|
|
|
|
for(fi = 0; done == 0; fi++){
|
|
|
|
char name[32];
|
|
|
|
name[0] = 'b';
|
|
|
|
name[1] = 'i';
|
|
|
|
name[2] = 'g';
|
|
|
|
name[3] = '0' + fi;
|
|
|
|
name[4] = '\0';
|
|
|
|
unlink(name);
|
|
|
|
int fd = open(name, O_CREATE|O_RDWR|O_TRUNC);
|
|
|
|
if(fd < 0){
|
2022-08-23 16:26:26 +00:00
|
|
|
// oops, ran out of inodes before running out of blocks.
|
2022-08-10 10:13:52 +00:00
|
|
|
printf("%s: could not create file %s\n", s, name);
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for(int i = 0; i < MAXFILE; i++){
|
|
|
|
char buf[BSIZE];
|
|
|
|
if(write(fd, buf, BSIZE) != BSIZE){
|
|
|
|
done = 1;
|
|
|
|
close(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2022-08-23 12:23:12 +00:00
|
|
|
// now that there are no free blocks, test that dirlink()
|
|
|
|
// merely fails (doesn't panic) if it can't extend
|
2022-08-23 16:26:26 +00:00
|
|
|
// directory content. one of these file creations
|
|
|
|
// is expected to fail.
|
2022-08-23 12:23:12 +00:00
|
|
|
int nzz = 128;
|
|
|
|
for(int i = 0; i < nzz; i++){
|
|
|
|
char name[32];
|
|
|
|
name[0] = 'z';
|
|
|
|
name[1] = 'z';
|
|
|
|
name[2] = '0' + (i / 32);
|
|
|
|
name[3] = '0' + (i % 32);
|
|
|
|
name[4] = '\0';
|
|
|
|
unlink(name);
|
|
|
|
int fd = open(name, O_CREATE|O_RDWR|O_TRUNC);
|
2022-08-23 16:26:26 +00:00
|
|
|
if(fd < 0)
|
2022-08-23 12:23:12 +00:00
|
|
|
break;
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2022-08-23 16:26:26 +00:00
|
|
|
// this mkdir() is expected to fail.
|
|
|
|
if(mkdir("diskfulldir") == 0)
|
|
|
|
printf("%s: mkdir(diskfulldir) unexpectedly succeeded!\n");
|
|
|
|
|
2022-08-23 12:52:15 +00:00
|
|
|
unlink("diskfulldir");
|
|
|
|
|
2022-08-23 12:23:12 +00:00
|
|
|
for(int i = 0; i < nzz; i++){
|
|
|
|
char name[32];
|
|
|
|
name[0] = 'z';
|
|
|
|
name[1] = 'z';
|
|
|
|
name[2] = '0' + (i / 32);
|
|
|
|
name[3] = '0' + (i % 32);
|
|
|
|
name[4] = '\0';
|
|
|
|
unlink(name);
|
|
|
|
}
|
|
|
|
|
2022-08-10 10:13:52 +00:00
|
|
|
for(int i = 0; i < fi; i++){
|
|
|
|
char name[32];
|
|
|
|
name[0] = 'b';
|
|
|
|
name[1] = 'i';
|
|
|
|
name[2] = 'g';
|
|
|
|
name[3] = '0' + i;
|
|
|
|
name[4] = '\0';
|
|
|
|
unlink(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 16:26:26 +00:00
|
|
|
void
|
|
|
|
outofinodes(char *s)
|
|
|
|
{
|
|
|
|
int nzz = 32*32;
|
|
|
|
for(int i = 0; i < nzz; i++){
|
|
|
|
char name[32];
|
|
|
|
name[0] = 'z';
|
|
|
|
name[1] = 'z';
|
|
|
|
name[2] = '0' + (i / 32);
|
|
|
|
name[3] = '0' + (i % 32);
|
|
|
|
name[4] = '\0';
|
|
|
|
unlink(name);
|
|
|
|
int fd = open(name, O_CREATE|O_RDWR|O_TRUNC);
|
|
|
|
if(fd < 0){
|
|
|
|
// failure is eventually expected.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < nzz; i++){
|
|
|
|
char name[32];
|
|
|
|
name[0] = 'z';
|
|
|
|
name[1] = 'z';
|
|
|
|
name[2] = '0' + (i / 32);
|
|
|
|
name[3] = '0' + (i % 32);
|
|
|
|
name[4] = '\0';
|
|
|
|
unlink(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 13:45:35 +00:00
|
|
|
struct test slowtests[] = {
|
|
|
|
{bigdir, "bigdir"},
|
|
|
|
{manywrites, "manywrites"},
|
|
|
|
{badwrite, "badwrite" },
|
|
|
|
{execout, "execout"},
|
|
|
|
{diskfull, "diskfull"},
|
|
|
|
{outofinodes, "outofinodes"},
|
|
|
|
|
|
|
|
{ 0, 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// drive tests
|
|
|
|
//
|
|
|
|
|
|
|
|
// run each test in its own process. run returns 1 if child's exit()
|
|
|
|
// indicates success.
|
|
|
|
int
|
|
|
|
run(void f(char *), char *s) {
|
|
|
|
int pid;
|
|
|
|
int xstatus;
|
|
|
|
|
|
|
|
printf("test %s: ", s);
|
|
|
|
if((pid = fork()) < 0) {
|
|
|
|
printf("runtest: fork error\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(pid == 0) {
|
|
|
|
f(s);
|
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
wait(&xstatus);
|
|
|
|
if(xstatus != 0)
|
|
|
|
printf("FAILED\n");
|
|
|
|
else
|
|
|
|
printf("OK\n");
|
|
|
|
return xstatus == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
runtests(struct test *tests, char *justone) {
|
|
|
|
for (struct test *t = tests; t->s != 0; t++) {
|
|
|
|
if((justone == 0) || strcmp(t->s, justone) == 0) {
|
|
|
|
if(!run(t->f, t->s)){
|
|
|
|
printf("SOME TESTS FAILED\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-28 09:58:28 +00:00
|
|
|
//
|
|
|
|
// use sbrk() to count how many free physical memory pages there are.
|
2020-08-27 10:21:10 +00:00
|
|
|
// touches the pages to force allocation.
|
|
|
|
// because out of memory with lazy allocation results in the process
|
|
|
|
// taking a fault and being killed, fork and report back.
|
2019-10-28 09:58:28 +00:00
|
|
|
//
|
|
|
|
int
|
|
|
|
countfree()
|
|
|
|
{
|
2020-08-27 10:21:10 +00:00
|
|
|
int fds[2];
|
|
|
|
|
|
|
|
if(pipe(fds) < 0){
|
|
|
|
printf("pipe() failed in countfree()\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pid = fork();
|
|
|
|
|
|
|
|
if(pid < 0){
|
|
|
|
printf("fork failed in countfree()\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pid == 0){
|
|
|
|
close(fds[0]);
|
|
|
|
|
|
|
|
while(1){
|
|
|
|
uint64 a = (uint64) sbrk(4096);
|
|
|
|
if(a == 0xffffffffffffffff){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// modify the memory to make sure it's really allocated.
|
|
|
|
*(char *)(a + 4096 - 1) = 1;
|
|
|
|
|
|
|
|
// report back one more page.
|
|
|
|
if(write(fds[1], "x", 1) != 1){
|
|
|
|
printf("write() failed in countfree()\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
2019-10-28 09:58:28 +00:00
|
|
|
|
2020-08-27 10:21:10 +00:00
|
|
|
close(fds[1]);
|
|
|
|
|
|
|
|
int n = 0;
|
2019-10-28 09:58:28 +00:00
|
|
|
while(1){
|
2020-08-27 10:21:10 +00:00
|
|
|
char c;
|
|
|
|
int cc = read(fds[0], &c, 1);
|
|
|
|
if(cc < 0){
|
|
|
|
printf("read() failed in countfree()\n");
|
|
|
|
exit(1);
|
2019-10-28 09:58:28 +00:00
|
|
|
}
|
2020-08-27 10:21:10 +00:00
|
|
|
if(cc == 0)
|
|
|
|
break;
|
2019-10-28 09:58:28 +00:00
|
|
|
n += 1;
|
|
|
|
}
|
2020-08-27 10:21:10 +00:00
|
|
|
|
|
|
|
close(fds[0]);
|
|
|
|
wait((int*)0);
|
|
|
|
|
2019-10-28 09:58:28 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2019-09-19 19:14:52 +00:00
|
|
|
int
|
2022-08-25 13:45:35 +00:00
|
|
|
drivetests(int quick, int continuous, char *justone) {
|
|
|
|
do {
|
|
|
|
printf("usertests starting\n");
|
|
|
|
int free0 = countfree();
|
|
|
|
int free1 = 0;
|
|
|
|
if (runtests(quicktests, justone)) {
|
|
|
|
if(continuous != 2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!quick) {
|
|
|
|
if (justone == 0)
|
|
|
|
printf("usertests slow tests starting\n");
|
|
|
|
if (runtests(slowtests, justone)) {
|
|
|
|
if(continuous != 2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if((free1 = countfree()) < free0) {
|
|
|
|
printf("FAILED -- lost some free pages %d (out of %d)\n", free1, free0);
|
|
|
|
if(continuous != 2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while(continuous);
|
|
|
|
return 0;
|
2019-07-22 22:08:52 +00:00
|
|
|
}
|
|
|
|
|
2006-07-16 15:36:31 +00:00
|
|
|
int
|
2006-07-28 22:33:07 +00:00
|
|
|
main(int argc, char *argv[])
|
2006-06-27 14:35:53 +00:00
|
|
|
{
|
2019-10-28 09:58:28 +00:00
|
|
|
int continuous = 0;
|
2022-08-25 13:45:35 +00:00
|
|
|
int quick = 0;
|
2019-10-28 09:58:28 +00:00
|
|
|
char *justone = 0;
|
|
|
|
|
2022-08-25 13:45:35 +00:00
|
|
|
if(argc == 2 && strcmp(argv[1], "-q") == 0){
|
|
|
|
quick = 1;
|
|
|
|
} else if(argc == 2 && strcmp(argv[1], "-c") == 0){
|
2019-10-28 09:58:28 +00:00
|
|
|
continuous = 1;
|
2020-08-13 14:22:07 +00:00
|
|
|
} else if(argc == 2 && strcmp(argv[1], "-C") == 0){
|
|
|
|
continuous = 2;
|
2019-10-28 09:58:28 +00:00
|
|
|
} else if(argc == 2 && argv[1][0] != '-'){
|
|
|
|
justone = argv[1];
|
|
|
|
} else if(argc > 1){
|
2022-08-25 13:45:35 +00:00
|
|
|
printf("Usage: usertests [-c] [-C] [-q] [testname]\n");
|
2019-10-28 09:58:28 +00:00
|
|
|
exit(1);
|
2019-09-19 10:58:28 +00:00
|
|
|
}
|
2022-08-25 13:45:35 +00:00
|
|
|
if (drivetests(quick, continuous, justone)) {
|
2019-10-28 09:58:28 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2022-08-25 13:45:35 +00:00
|
|
|
printf("ALL TESTS PASSED\n");
|
|
|
|
exit(0);
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|