fix exec argc

This commit is contained in:
Robert Morris 2019-06-04 06:45:09 -04:00
parent 8baac76050
commit 0f684b9150
3 changed files with 16 additions and 16 deletions

2
README
View file

@ -1,6 +1,6 @@
xv6 is a re-implementation of Dennis Ritchie's and Ken Thompson's Unix xv6 is a re-implementation of Dennis Ritchie's and Ken Thompson's Unix
Version 6 (v6). xv6 loosely follows the structure and style of v6, Version 6 (v6). xv6 loosely follows the structure and style of v6,
but is implemented for a modern x86-based multiprocessor using ANSI C. but is implemented for a modern RISC-V multiprocessor using ANSI C.
ACKNOWLEDGMENTS ACKNOWLEDGMENTS

View file

@ -205,7 +205,7 @@ consolewrite(struct inode *ip, int user_src, uint64 src, int n)
acquire(&cons.lock); acquire(&cons.lock);
for(i = 0; i < n; i++){ for(i = 0; i < n; i++){
char c; char c;
if(either_copyin(&c, user_src, src, 1) == -1) if(either_copyin(&c, user_src, src+i, 1) == -1)
break; break;
consputc(c); consputc(c);
} }

28
exec.c
View file

@ -13,7 +13,7 @@ exec(char *path, char **argv)
{ {
char *s, *last; char *s, *last;
int i, off; int i, off;
uint64 argc, sz, sp, ustack[3+MAXARG+1]; uint64 argc, sz, sp, ustack[MAXARG+1];
struct elfhdr elf; struct elfhdr elf;
struct inode *ip; struct inode *ip;
struct proghdr ph; struct proghdr ph;
@ -71,24 +71,24 @@ exec(char *path, char **argv)
for(argc = 0; argv[argc]; argc++) { for(argc = 0; argv[argc]; argc++) {
if(argc >= MAXARG) if(argc >= MAXARG)
goto bad; goto bad;
sp = (sp - (strlen(argv[argc]) + 1)) & ~(sizeof(uint64)-1); sp -= strlen(argv[argc]) + 1;
sp -= sp % 16; // riscv sp must be 16-byte aligned
if(copyout(pagetable, sp, argv[argc], strlen(argv[argc]) + 1) < 0) if(copyout(pagetable, sp, argv[argc], strlen(argv[argc]) + 1) < 0)
goto bad; goto bad;
ustack[3+argc] = sp; ustack[argc] = sp;
} }
ustack[3+argc] = 0; ustack[argc] = 0;
ustack[0] = 0xffffffff; // fake return PC // push the array of argv[] pointers.
ustack[1] = argc; sp -= (argc+1) * sizeof(uint64);
ustack[2] = sp - (argc+1)*sizeof(uint64); // argv pointer sp -= sp % 16;
if(copyout(pagetable, sp, (char *)ustack, (argc+1)*sizeof(uint64)) < 0)
goto bad;
// arguments to user main(argc, argv) // arguments to user main(argc, argv)
p->tf->a0 = argc; // argc is returned via the system call return
p->tf->a1 = sp - (argc+1)*sizeof(uint64); // value, which goes in a0.
p->tf->a1 = sp;
sp -= (3+argc+1) * sizeof(uint64);
if(copyout(pagetable, sp, (char *)ustack, (3+argc+1)*sizeof(uint64)) < 0)
goto bad;
// Save program name for debugging. // Save program name for debugging.
for(last=s=path; *s; s++) for(last=s=path; *s; s++)
@ -103,7 +103,7 @@ exec(char *path, char **argv)
p->tf->epc = elf.entry; // initial program counter = main p->tf->epc = elf.entry; // initial program counter = main
p->tf->sp = sp; // initial stack pointer p->tf->sp = sp; // initial stack pointer
proc_freepagetable(oldpagetable, oldsz); proc_freepagetable(oldpagetable, oldsz);
return 0; return argc; // this ends up in a0, the first argument to main(argc, argv)
bad: bad:
if(pagetable) if(pagetable)