Skip to content

Commit

Permalink
init creates console, opens 0/1/2, runs sh
Browse files Browse the repository at this point in the history
sh accepts 0-argument commands (like userfs)
reads from console
  • Loading branch information
rtm committed Aug 11, 2006
1 parent 5be0039 commit 17a8565
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 39 deletions.
18 changes: 13 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ bootblock : bootasm.S bootmain.c
$(OBJCOPY) -S -O binary bootblock.o bootblock
./sign.pl bootblock

kernel : $(OBJS) bootother.S user1 usertests userfs
kernel : $(OBJS) bootother.S userfs init
$(CC) -nostdinc -I. -c bootother.S
$(LD) -N -e start -Ttext 0x7000 -o bootother.out bootother.o
$(OBJCOPY) -S -O binary bootother.out bootother
$(OBJDUMP) -S bootother.o > bootother.asm
$(LD) -Ttext 0x100000 -e main0 -o kernel $(OBJS) -b binary bootother user1 usertests userfs
$(LD) -Ttext 0x100000 -e main0 -o kernel $(OBJS) -b binary bootother userfs init
$(OBJDUMP) -S kernel > kernel.asm

vectors.S : vectors.pl
Expand Down Expand Up @@ -79,15 +79,23 @@ userfs : userfs.o $(ULIB)
$(LD) -N -e main -Ttext 0 -o userfs userfs.o $(ULIB)
$(OBJDUMP) -S userfs > userfs.asm

init : init.o $(ULIB)
$(LD) -N -e main -Ttext 0 -o init init.o $(ULIB)
$(OBJDUMP) -S init > init.asm

sh : sh.o $(ULIB)
$(LD) -N -e main -Ttext 0 -o sh sh.o $(ULIB)
$(OBJDUMP) -S sh > sh.asm

mkfs : mkfs.c fs.h
cc -o mkfs mkfs.c

fs.img : mkfs usertests echo cat README
./mkfs fs.img usertests echo cat README
fs.img : mkfs userfs usertests echo cat README init sh
./mkfs fs.img userfs usertests echo cat README init sh

-include *.d

clean :
rm -f *.o *.d *.asm vectors.S parport.out \
bootblock kernel xv6.img user1 userfs usertests \
fs.img mkfs echo
fs.img mkfs echo init
32 changes: 29 additions & 3 deletions console.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,14 @@ panic(char *s)
}

int
console_write (int minor, void *buf, int n)
console_write (int minor, char *buf, int n)
{
int i;
uchar *b = buf;

acquire(&console_lock);

for (i = 0; i < n; i++) {
cons_putc((int) b[i]);
cons_putc(buf[i] & 0xff);
}

release(&console_lock);
Expand Down Expand Up @@ -349,20 +348,47 @@ kbd_intr()
kbd_buf[kbd_w++] = c;
if(kbd_w >= KBD_BUF)
kbd_w = 0;
wakeup(&kbd_r);
} else {
cprintf("kbd overflow\n");
}

release(&kbd_lock);
}

int
console_read(int minor, char *dst, int n)
{
uint target = n;

acquire(&kbd_lock);

while(kbd_w == kbd_r)
sleep(&kbd_r, &kbd_lock);

while(n > 0 && kbd_w != kbd_r){
*dst = kbd_buf[kbd_r];
cons_putc(*dst & 0xff);
dst++;
--n;
kbd_r++;
if(kbd_r >= KBD_BUF)
kbd_r = 0;
}

release(&kbd_lock);

return target - n;
}

void
console_init()
{
initlock(&console_lock, "console");
initlock(&kbd_lock, "kbd");

devsw[CONSOLE].d_write = console_write;
devsw[CONSOLE].d_read = console_read;

ioapic_enable (IRQ_KBD, 1);

Expand Down
5 changes: 3 additions & 2 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ void iunlock(struct inode *ip);
void idecref(struct inode *ip);
void iput(struct inode *ip);
struct inode * namei(char *path);
int readi(struct inode *ip, void *xdst, uint off, uint n);
int writei(struct inode *ip, void *addr, uint off, uint n);
int readi(struct inode *ip, char *xdst, uint off, uint n);
int writei(struct inode *ip, char *addr, uint off, uint n);
struct inode *mknod(struct inode *, char *, short, short, short);
void iupdate (struct inode *ip);
4 changes: 2 additions & 2 deletions dev.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
struct devsw {
int (*d_open)(char *, int);
int (*d_read)(int, void *, int);
int (*d_write)(int, void *, int);
int (*d_read)(int, char *, int);
int (*d_write)(int, char *, int);
int (*d_close)(int);
};

Expand Down
11 changes: 6 additions & 5 deletions fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ iget(uint dev, uint inum)
goto loop;
}
ip->count++;
ip->busy = 1;
release(&inode_table_lock);
return ip;
}
Expand Down Expand Up @@ -269,16 +270,15 @@ bmap(struct inode *ip, uint bn)
#define min(a, b) ((a) < (b) ? (a) : (b))

int
readi(struct inode *ip, void *xdst, uint off, uint n)
readi(struct inode *ip, char *dst, uint off, uint n)
{
char *dst = (char *) xdst;
uint target = n, n1;
struct buf *bp;

if (ip->type == T_DEV) {
if (ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].d_read)
return -1;
return devsw[ip->major].d_read (ip->minor, xdst, n);
return devsw[ip->major].d_read (ip->minor, dst, n);
}

while(n > 0 && off < ip->size){
Expand All @@ -298,7 +298,7 @@ readi(struct inode *ip, void *xdst, uint off, uint n)
#define MIN(a, b) ((a < b) ? a : b)

int
writei(struct inode *ip, void *addr, uint off, uint n)
writei(struct inode *ip, char *addr, uint off, uint n)
{
if (ip->type == T_DEV) {
if (ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].d_write)
Expand Down Expand Up @@ -404,7 +404,8 @@ mknod(struct inode *dp, char *cp, short type, short major, short minor)
int i;
struct buf *bp = 0;

cprintf("mknod: %s %d %d %d\n", cp, type, major, minor);
cprintf("mknod: dir %d %s %d %d %d\n",
dp->inum, cp, type, major, minor);

ip = ialloc(dp->dev, type);
if (ip == 0) return 0;
Expand Down
32 changes: 32 additions & 0 deletions init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "user.h"
#include "types.h"
#include "fs.h"
#include "fcntl.h"

char *sh_args[] = { "sh", 0 };

int
main(void)
{
int pid;

if(open("console", 0) < 0){
mknod("console", T_DEV, 1, 1);
open("console", 0);
}
open("console", 1);
open("console", 1);

write(1, "init...\n", 8);

while(1){
write(1, "running sh...\n", 14);
pid = fork();
if(pid == 0){
exec("sh", sh_args);
exit();
}
if(pid > 0)
wait();
}
}
8 changes: 4 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
#include "spinlock.h"

extern char edata[], end[];
extern uchar _binary_user1_start[], _binary_user1_size[];
extern uchar _binary_usertests_start[], _binary_usertests_size[];
extern uchar _binary_userfs_start[], _binary_userfs_size[];
extern uchar _binary_init_start[], _binary_init_size[];

// CPU 0 starts running C code here.
// This is called main0 not main so that it can have
Expand Down Expand Up @@ -96,9 +95,10 @@ main0(void)
p = copyproc(&proc[0]);

//load_icode(p, _binary_usertests_start, (uint) _binary_usertests_size);
load_icode(p, _binary_userfs_start, (uint) _binary_userfs_size);
//load_icode(p, _binary_userfs_start, (uint) _binary_userfs_size);
load_icode(p, _binary_init_start, (uint) _binary_init_size);
p->state = RUNNABLE;
cprintf("loaded userfs\n");
cprintf("loaded init\n");

scheduler();
}
Expand Down
31 changes: 31 additions & 0 deletions sh.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "user.h"
#include "types.h"
#include "fs.h"
#include "fcntl.h"

char *args[100];

int
main(void)
{
char buf[128];
int pid;

while(1){
write(1, "$ ", 2);
gets(buf, sizeof(buf));
if(buf[0] == '\0')
continue;
pid = fork();
if(pid == 0){
args[0] = buf;
args[1] = 0;
exec(buf, args);
write(1, buf, strlen(buf));
write(1, ": not found\n", 12);
exit();
}
if(pid > 0)
wait();
}
}
17 changes: 12 additions & 5 deletions syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,19 @@ sys_mknod(void)
return -1;

dp = iget(rootdev, 1); // XXX should parse name
if (dp->type != T_DIR)
if (dp->type != T_DIR) {
iput(dp);
return -1;
}

nip = mknod (dp, cp->mem + arg0, (short) arg1, (short) arg2,
(short) arg3);

iput(dp);

if (nip == 0) return -1;

iput(nip);
iput(dp);

return 0;
}
Expand Down Expand Up @@ -376,15 +381,16 @@ sys_exec(void)
if(ip == 0)
return -1;

if(readi(ip, &elf, 0, sizeof(elf)) < sizeof(elf))
if(readi(ip, (char*)&elf, 0, sizeof(elf)) < sizeof(elf))
goto bad;

if(elf.magic != ELF_MAGIC)
goto bad;

sz = 0;
for(i = 0; i < elf.phnum; i++){
if(readi(ip, &ph, elf.phoff + i * sizeof(ph), sizeof(ph)) != sizeof(ph))
if(readi(ip, (char*)&ph, elf.phoff + i * sizeof(ph),
sizeof(ph)) != sizeof(ph))
goto bad;
if(ph.type != ELF_PROG_LOAD)
continue;
Expand Down Expand Up @@ -450,7 +456,8 @@ sys_exec(void)
mem = 0;

for(i = 0; i < elf.phnum; i++){
if(readi(ip, &ph, elf.phoff + i * sizeof(ph), sizeof(ph)) != sizeof(ph))
if(readi(ip, (char*)&ph, elf.phoff + i * sizeof(ph),
sizeof(ph)) != sizeof(ph))
goto bad2;
if(ph.type != ELF_PROG_LOAD)
continue;
Expand Down
39 changes: 28 additions & 11 deletions ulib.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@
int
puts(char *s)
{
return cons_puts(s);
}

int
puts1(char *s)
{
int i;

for(i = 0; s[i]; i++)
cons_putc(s[i]);
return i;
return write(1, s, strlen(s));
}

char*
Expand All @@ -26,3 +16,30 @@ strcpy(char *s, char *t)
;
return os;
}

unsigned int
strlen(char *s)
{
int n = 0;
for(n = 0; s[n]; n++)
;
return n;
}

char *
gets(char *buf, int max)
{
int i = 0, cc;
char c;

while(i+1 < max){
cc = read(0, &c, 1);
if(cc < 1)
break;
if(c == '\n' || c == '\r')
break;
buf[i++] = c;
}
buf[i] = '\0';
return buf;
}
5 changes: 3 additions & 2 deletions user.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ int exec(char *, char **);
int open(char *, int);
int mknod (char*,short,short,short);
int unlink (char*);

int puts(char*);
int puts1(char*);
char* strcpy(char*, char*);
void printf(int fd, char *fmt, ...);

char *gets(char *, int max);
unsigned int strlen(char *);

0 comments on commit 17a8565

Please sign in to comment.