Skip to content

Commit

Permalink
transplant 2048
Browse files Browse the repository at this point in the history
  • Loading branch information
HCY-ASLEEP committed Jun 3, 2023
1 parent b299df4 commit 7384d0c
Show file tree
Hide file tree
Showing 18 changed files with 1,355 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ mkfs
kernel/kernel
user/usys.S
.gdbinit
compile_commands.json
.cache
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
K=kernel
U=user
L=lib

OBJS = \
$K/entry.o \
Expand Down Expand Up @@ -88,6 +89,7 @@ tags: $(OBJS) _init
etags *.S *.c

ULIB = $U/ulib.o $U/usys.o $U/printf.o $U/umalloc.o
LIBC = $L/libc.o $U/usys.o

_%: %.o $(ULIB)
$(LD) $(LDFLAGS) -T $U/user.ld -o $@ $^
Expand All @@ -106,6 +108,10 @@ $U/_forktest: $U/forktest.o $(ULIB)
$(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $U/_forktest $U/forktest.o $U/ulib.o $U/usys.o
$(OBJDUMP) -S $U/_forktest > $U/forktest.asm

$U/_2048: $U/2048.o $(LIBC)
$(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $U/_2048 $U/2048.o $L/libc.o $U/usys.o
$(OBJDUMP) -S $U/_2048 > $U/2048.asm

mkfs/mkfs: mkfs/mkfs.c $K/fs.h $K/param.h
gcc -Werror -Wall -I. -o mkfs/mkfs mkfs/mkfs.c

Expand All @@ -132,6 +138,7 @@ UPROGS=\
$U/_grind\
$U/_wc\
$U/_zombie\
$U/_2048\

fs.img: mkfs/mkfs README $(UPROGS)
mkfs/mkfs fs.img README $(UPROGS)
Expand All @@ -157,7 +164,6 @@ CPUS := 3
endif

QEMUOPTS = -machine virt -bios none -kernel $K/kernel -m 128M -smp $(CPUS) -nographic
QEMUOPTS += -global virtio-mmio.force-legacy=false
QEMUOPTS += -drive file=fs.img,if=none,format=raw,id=x0
QEMUOPTS += -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0

Expand Down
17 changes: 17 additions & 0 deletions include/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#ifndef _UCC_ASSERT_H
#define _UCC_ASSERT_H

#ifdef NDEBUG

#define assert(expr) ((void) 0)

#else

void __assert_fail(const char *, const char *, int);
#define assert(expr) \
((expr) ? (void) 0 : __assert_fail(#expr, __FILE__, __LINE__))

#endif

#endif /* assert.h */
13 changes: 13 additions & 0 deletions include/ctype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _UCC_CTYPE_H
#define _UCC_CTYPE_H

int isalnum(int);
int isalpha(int);
int isdigit(int);
int islower(int);
int isspace(int);
int isupper(int);
int tolower(int);
int toupper(int);

#endif /* ctype.h */
20 changes: 20 additions & 0 deletions include/limits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef _UCC_LIMITS_H
#define _UCC_LIMITS_H

#define CHAR_BIT 8
#define SCHAR_MAX 127
#define SCHAR_MIN (-128)
#define UCHAR_MAX 255
#define CHAR_MAX 127
#define CHAR_MIN (-128)
#define SHRT_MAX 32767
#define SHRT_MIN (-32768)
#define USHRT_MAX 65535
#define INT_MAX 2147483647
#define INT_MIN (-2147483648)
#define UINT_MAX 4294967295
#define LONG_MAX INT_MAX
#define LONG_MIN INT_MIN
#define ULONG_MAX UINT_MAX

#endif /* limits.h */
9 changes: 9 additions & 0 deletions include/stdarg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _UCC_STDARG_H
#define _UCC_STDARG_H

#define va_arg(ap, type) (*((type *)(ap = (type *)ap + 1) - 1))
#define va_start(ap, arg) (ap = &(arg) + 1)
#define va_end(ap) ((void) 0)
typedef void *va_list;

#endif /* stdarg.h */
9 changes: 9 additions & 0 deletions include/stdbool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _UCC_STDBOOL_H
#define _UCC_STDBOOL_H

#define bool int
#define true 1
#define false 0
#define __bool_true_false_are_defined 1

#endif /* stdbool.h */
9 changes: 9 additions & 0 deletions include/stddef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _UCC_STDDEF_H
#define _UCC_STDDEF_H

#define NULL 0
typedef unsigned size_tt;
typedef int ssize_t;
typedef int ptrdiff_tt;

#endif /* stddef.h */
66 changes: 66 additions & 0 deletions include/stdio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#ifndef _UCC_STDIO_H
#define _UCC_STDIO_H

#include <stdarg.h>
#include "include/stddef.h"

#ifdef NULL
#undef NULL
#endif

#define NULL 0
#define EOF (-1)
#define BUFSIZ 1024
#define OPEN_MAX 10

typedef struct _iobuf {
int cnt; /* characters left */
char *ptr; /* next character position */
char *base; /* location of the buffer */
int flag; /* mode of the file access */
int fd; /* file descriptor */
} FILE;

extern FILE _iob[OPEN_MAX];

#define stdin (&_iob[0])
#define stdout (&_iob[1])
#define stderr (&_iob[2])

enum _flags {
_READ = 01,
_WRITE = 02,
_UNBUF = 04,
_EOF = 010,
_ERR = 020,
_LNBUF = 040
};

int _fillbuf(FILE *);
int _flushbuf(int, FILE *);
int fflush(FILE *);

#define feof(p) (((p)->flag & _EOF) != 0)
#define ferror(p) (((p)->flag & _ERR) != 0)
#define fileno(p) ((p)->fd)

#define getc(p) fgetc(p)
#define putc(x, p) fputc(x, p)
#define getchar() getc(stdin)
#define putchar(x) putc((x), stdout)


int fputc(int, FILE *);
int fgetc(FILE *);
int puts(char *);
int fputs(char *, FILE *);
char *fgets(char *, int, FILE *);
char *gets(char *);


int printf(const char *, ...);
int fprintf(FILE *, const char *, ...);
int vprintf(const char *, va_list);
int vfprintf(FILE *, const char *, va_list);

#endif /* stdio.h */
19 changes: 19 additions & 0 deletions include/stdlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef _UCC_STDLIB_H
#define _UCC_STDLIB_H

#include "include/stddef.h"

#define RAND_MAX 2147483647

void abort(void);
int abs(int);
long strtol(const char *, char **, int);
int rand(void);
void srand(unsigned);
void *malloc(size_tt);
void *realloc(void *, size_tt);
void *calloc(size_tt, size_tt);
void free(void *);
void exit(int);

#endif /* stdlib.h */
16 changes: 16 additions & 0 deletions include/string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef _UCC_STRING_H
#define _UCC_STRING_H

#include <stddef.h>

size_tt strlen(const char *);
int strcmp(const char *, const char *);
int strncmp(const char *, const char *, size_tt);
char *strchr(const char *, int);
char *strcpy(char *, const char *);
char *strncpy(char *, const char *, size_tt);
void *memset(void *, int, size_tt);
void *memcpy(void *, const void *, size_tt);
void *memmove(void*, const void*, size_tt);

#endif /* string.h */
28 changes: 28 additions & 0 deletions include/termios.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#ifndef _UCC_TERMIOS_H
#define _UCC_TERMIOS_H

typedef unsigned int tcflag_t;
typedef unsigned char cc_t;

#define NCCS 32
struct termios {
tcflag_t c_iflag; /* input modes */
tcflag_t c_oflag; /* output modes */
tcflag_t c_cflag; /* control modes */
tcflag_t c_lflag; /* local modes */
cc_t c_cc[NCCS]; /* special characters */
};

int tcgetattr(int, struct termios *);
void cfmakeraw(struct termios *);
int tcsetattr(int, int, const struct termios *);

/* c_lflag bits */
#define ECHO 0000010
#define ICANON 0000002

/* tcsetattr uses these */
#define TCSANOW 0

#endif
35 changes: 35 additions & 0 deletions include/unistd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef _XV6_UNISTD_H
#define _XV6_UNISTD_H

#include "include/stddef.h"

struct stat;
struct rtcdate;

ssize_t write(int, const void *, size_tt);
ssize_t read(int, void *, size_tt);
int close(int);

int fork(void);
// int exit(void);
int wait(void);
int pipe(int*);
int kill(int);
int exec(char*, char**);
#ifndef _UCC_FCNTL_H
int open(char*, int);
#endif
int mknod(char*, short, short);
int unlink(char*);
int fstat(int fd, struct stat*);
int link(char*, char*);
int mkdir(char*);
int chdir(char*);
int dup(int);
int getpid(void);
char* sbrk(int);
int sleep(int);
int uptime(void);
int halt(void);

#endif /* unistd.h */
8 changes: 8 additions & 0 deletions kernel/date.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
struct rtcdate {
uint second;
uint minute;
uint hour;
uint day;
uint month;
uint year;
};
3 changes: 3 additions & 0 deletions kernel/virtio.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#define VIRTIO_MMIO_DEVICE_DESC_LOW 0x0a0 // physical address for used ring, write-only
#define VIRTIO_MMIO_DEVICE_DESC_HIGH 0x0a4

#define VIRTIO_MMIO_QUEUE_PFN 0x040 // physical page number for queue, read/write
#define VIRTIO_MMIO_GUEST_PAGE_SIZE 0x028 // page size for PFN, write-only

// status register bits, from qemu virtio_config.h
#define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
#define VIRTIO_CONFIG_S_DRIVER 2
Expand Down
Loading

0 comments on commit 7384d0c

Please sign in to comment.