update name of code to labcodes

This commit is contained in:
chyyuu
2013-09-17 22:21:48 +08:00
parent 759eca9dda
commit 3f8d5876b9
726 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
#include <defs.h>
#include <string.h>
#include <syscall.h>
#include <stat.h>
#include <dirent.h>
#include <file.h>
#include <dir.h>
#include <error.h>
#include <unistd.h>
DIR dir, *dirp=&dir;
DIR *
opendir(const char *path) {
if ((dirp->fd = open(path, O_RDONLY)) < 0) {
goto failed;
}
struct stat __stat, *stat = &__stat;
if (fstat(dirp->fd, stat) != 0 || !S_ISDIR(stat->st_mode)) {
goto failed;
}
dirp->dirent.offset = 0;
return dirp;
failed:
return NULL;
}
struct dirent *
readdir(DIR *dirp) {
if (sys_getdirentry(dirp->fd, &(dirp->dirent)) == 0) {
return &(dirp->dirent);
}
return NULL;
}
void
closedir(DIR *dirp) {
close(dirp->fd);
}
int
getcwd(char *buffer, size_t len) {
return sys_getcwd(buffer, len);
}

View File

@@ -0,0 +1,19 @@
#ifndef __USER_LIBS_DIR_H__
#define __USER_LIBS_DIR_H__
#include <defs.h>
#include <dirent.h>
typedef struct {
int fd;
struct dirent dirent;
} DIR;
DIR *opendir(const char *path);
struct dirent *readdir(DIR *dirp);
void closedir(DIR *dirp);
int chdir(const char *path);
int getcwd(char *buffer, size_t len);
#endif /* !__USER_LIBS_DIR_H__ */

View File

@@ -0,0 +1,68 @@
#include <defs.h>
#include <string.h>
#include <syscall.h>
#include <stdio.h>
#include <stat.h>
#include <error.h>
#include <unistd.h>
int
open(const char *path, uint32_t open_flags) {
return sys_open(path, open_flags);
}
int
close(int fd) {
return sys_close(fd);
}
int
read(int fd, void *base, size_t len) {
return sys_read(fd, base, len);
}
int
write(int fd, void *base, size_t len) {
return sys_write(fd, base, len);
}
int
seek(int fd, off_t pos, int whence) {
return sys_seek(fd, pos, whence);
}
int
fstat(int fd, struct stat *stat) {
return sys_fstat(fd, stat);
}
int
fsync(int fd) {
return sys_fsync(fd);
}
int
dup2(int fd1, int fd2) {
return sys_dup(fd1, fd2);
}
static char
transmode(struct stat *stat) {
uint32_t mode = stat->st_mode;
if (S_ISREG(mode)) return 'r';
if (S_ISDIR(mode)) return 'd';
if (S_ISLNK(mode)) return 'l';
if (S_ISCHR(mode)) return 'c';
if (S_ISBLK(mode)) return 'b';
return '-';
}
void
print_stat(const char *name, int fd, struct stat *stat) {
cprintf("[%03d] %s\n", fd, name);
cprintf(" mode : %c\n", transmode(stat));
cprintf(" links : %lu\n", stat->st_nlinks);
cprintf(" blocks : %lu\n", stat->st_blocks);
cprintf(" size : %lu\n", stat->st_size);
}

View File

@@ -0,0 +1,23 @@
#ifndef __USER_LIBS_FILE_H__
#define __USER_LIBS_FILE_H__
#include <defs.h>
struct stat;
int open(const char *path, uint32_t open_flags);
int close(int fd);
int read(int fd, void *base, size_t len);
int write(int fd, void *base, size_t len);
int seek(int fd, off_t pos, int whence);
int fstat(int fd, struct stat *stat);
int fsync(int fd);
int dup(int fd);
int dup2(int fd1, int fd2);
int pipe(int *fd_store);
int mkfifo(const char *name, uint32_t open_flags);
void print_stat(const char *name, int fd, struct stat *stat);
#endif /* !__USER_LIBS_FILE_H__ */

View File

@@ -0,0 +1,24 @@
.text
.globl _start
_start:
# set ebp for backtrace
movl $0x0, %ebp
# load argc and argv
movl (%esp), %ebx
lea 0x4(%esp), %ecx
# move down the esp register
# since it may cause page fault in backtrace
subl $0x20, %esp
# save argc and argv on stack
pushl %ecx
pushl %ebx
# call user-program function
call umain
1: jmp 1b

View File

@@ -0,0 +1,42 @@
#ifndef __USER_LIBS_LOCK_H__
#define __USER_LIBS_LOCK_H__
#include <defs.h>
#include <atomic.h>
#include <ulib.h>
#define INIT_LOCK {0}
typedef volatile bool lock_t;
static inline void
lock_init(lock_t *l) {
*l = 0;
}
static inline bool
try_lock(lock_t *l) {
return test_and_set_bit(0, l);
}
static inline void
lock(lock_t *l) {
if (try_lock(l)) {
int step = 0;
do {
yield();
if (++ step == 100) {
step = 0;
sleep(10);
}
} while (try_lock(l));
}
}
static inline void
unlock(lock_t *l) {
test_and_clear_bit(0, l);
}
#endif /* !__USER_LIBS_LOCK_H__ */

View File

@@ -0,0 +1,28 @@
#include <defs.h>
#include <stdarg.h>
#include <stdio.h>
#include <ulib.h>
#include <error.h>
void
__panic(const char *file, int line, const char *fmt, ...) {
// print the 'message'
va_list ap;
va_start(ap, fmt);
cprintf("user panic at %s:%d:\n ", file, line);
vcprintf(fmt, ap);
cprintf("\n");
va_end(ap);
exit(-E_PANIC);
}
void
__warn(const char *file, int line, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
cprintf("user warning at %s:%d:\n ", file, line);
vcprintf(fmt, ap);
cprintf("\n");
va_end(ap);
}

View File

@@ -0,0 +1,89 @@
#include <defs.h>
#include <stdio.h>
#include <syscall.h>
#include <file.h>
#include <ulib.h>
#include <unistd.h>
/* *
* cputch - writes a single character @c to stdout, and it will
* increace the value of counter pointed by @cnt.
* */
static void
cputch(int c, int *cnt) {
sys_putc(c);
(*cnt) ++;
}
/* *
* vcprintf - format a string and writes it to stdout
*
* The return value is the number of characters which would be
* written to stdout.
*
* Call this function if you are already dealing with a va_list.
* Or you probably want cprintf() instead.
* */
int
vcprintf(const char *fmt, va_list ap) {
int cnt = 0;
vprintfmt((void*)cputch, NO_FD, &cnt, fmt, ap);
return cnt;
}
/* *
* cprintf - formats a string and writes it to stdout
*
* The return value is the number of characters which would be
* written to stdout.
* */
int
cprintf(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
int cnt = vcprintf(fmt, ap);
va_end(ap);
return cnt;
}
/* *
* cputs- writes the string pointed by @str to stdout and
* appends a newline character.
* */
int
cputs(const char *str) {
int cnt = 0;
char c;
while ((c = *str ++) != '\0') {
cputch(c, &cnt);
}
cputch('\n', &cnt);
return cnt;
}
static void
fputch(char c, int *cnt, int fd) {
write(fd, &c, sizeof(char));
(*cnt) ++;
}
int
vfprintf(int fd, const char *fmt, va_list ap) {
int cnt = 0;
vprintfmt((void*)fputch, fd, &cnt, fmt, ap);
return cnt;
}
int
fprintf(int fd, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
int cnt = vfprintf(fd, fmt, ap);
va_end(ap);
return cnt;
}

View File

@@ -0,0 +1,145 @@
#include <defs.h>
#include <unistd.h>
#include <stdarg.h>
#include <syscall.h>
#include <stat.h>
#include <dirent.h>
#define MAX_ARGS 5
static inline int
syscall(int num, ...) {
va_list ap;
va_start(ap, num);
uint32_t a[MAX_ARGS];
int i, ret;
for (i = 0; i < MAX_ARGS; i ++) {
a[i] = va_arg(ap, uint32_t);
}
va_end(ap);
asm volatile (
"int %1;"
: "=a" (ret)
: "i" (T_SYSCALL),
"a" (num),
"d" (a[0]),
"c" (a[1]),
"b" (a[2]),
"D" (a[3]),
"S" (a[4])
: "cc", "memory");
return ret;
}
int
sys_exit(int error_code) {
return syscall(SYS_exit, error_code);
}
int
sys_fork(void) {
return syscall(SYS_fork);
}
int
sys_wait(int pid, int *store) {
return syscall(SYS_wait, pid, store);
}
int
sys_yield(void) {
return syscall(SYS_yield);
}
int
sys_kill(int pid) {
return syscall(SYS_kill, pid);
}
int
sys_getpid(void) {
return syscall(SYS_getpid);
}
int
sys_putc(int c) {
return syscall(SYS_putc, c);
}
int
sys_pgdir(void) {
return syscall(SYS_pgdir);
}
void
sys_lab6_set_priority(uint32_t priority)
{
syscall(SYS_lab6_set_priority, priority);
}
int
sys_sleep(unsigned int time) {
return syscall(SYS_sleep, time);
}
int
sys_gettime(void) {
return syscall(SYS_gettime);
}
int
sys_exec(const char *name, int argc, const char **argv) {
return syscall(SYS_exec, name, argc, argv);
}
int
sys_open(const char *path, uint32_t open_flags) {
return syscall(SYS_open, path, open_flags);
}
int
sys_close(int fd) {
return syscall(SYS_close, fd);
}
int
sys_read(int fd, void *base, size_t len) {
return syscall(SYS_read, fd, base, len);
}
int
sys_write(int fd, void *base, size_t len) {
return syscall(SYS_write, fd, base, len);
}
int
sys_seek(int fd, off_t pos, int whence) {
return syscall(SYS_seek, fd, pos, whence);
}
int
sys_fstat(int fd, struct stat *stat) {
return syscall(SYS_fstat, fd, stat);
}
int
sys_fsync(int fd) {
return syscall(SYS_fsync, fd);
}
int
sys_getcwd(char *buffer, size_t len) {
return syscall(SYS_getcwd, buffer, len);
}
int
sys_getdirentry(int fd, struct dirent *dirent) {
return syscall(SYS_getdirentry, fd, dirent);
}
int
sys_dup(int fd1, int fd2) {
return syscall(SYS_dup, fd1, fd2);
}

View File

@@ -0,0 +1,33 @@
#ifndef __USER_LIBS_SYSCALL_H__
#define __USER_LIBS_SYSCALL_H__
int sys_exit(int error_code);
int sys_fork(void);
int sys_wait(int pid, int *store);
int sys_exec(const char *name, int argc, const char **argv);
int sys_yield(void);
int sys_kill(int pid);
int sys_getpid(void);
int sys_putc(int c);
int sys_pgdir(void);
int sys_sleep(unsigned int time);
int sys_gettime(void);
struct stat;
struct dirent;
int sys_open(const char *path, uint32_t open_flags);
int sys_close(int fd);
int sys_read(int fd, void *base, size_t len);
int sys_write(int fd, void *base, size_t len);
int sys_seek(int fd, off_t pos, int whence);
int sys_fstat(int fd, struct stat *stat);
int sys_fsync(int fd);
int sys_getcwd(char *buffer, size_t len);
int sys_getdirentry(int fd, struct dirent *dirent);
int sys_dup(int fd1, int fd2);
void sys_lab6_set_priority(uint32_t priority); //only for lab6
#endif /* !__USER_LIBS_SYSCALL_H__ */

View File

@@ -0,0 +1,87 @@
#include <defs.h>
#include <syscall.h>
#include <stdio.h>
#include <ulib.h>
#include <stat.h>
#include <string.h>
#include <lock.h>
static lock_t fork_lock = INIT_LOCK;
void
lock_fork(void) {
lock(&fork_lock);
}
void
unlock_fork(void) {
unlock(&fork_lock);
}
void
exit(int error_code) {
sys_exit(error_code);
cprintf("BUG: exit failed.\n");
while (1);
}
int
fork(void) {
return sys_fork();
}
int
wait(void) {
return sys_wait(0, NULL);
}
int
waitpid(int pid, int *store) {
return sys_wait(pid, store);
}
void
yield(void) {
sys_yield();
}
int
kill(int pid) {
return sys_kill(pid);
}
int
getpid(void) {
return sys_getpid();
}
//print_pgdir - print the PDT&PT
void
print_pgdir(void) {
sys_pgdir();
}
void
lab6_set_priority(uint32_t priority)
{
sys_lab6_set_priority(priority);
}
int
sleep(unsigned int time) {
return sys_sleep(time);
}
unsigned int
gettime_msec(void) {
return (unsigned int)sys_gettime();
}
int
__exec(const char *name, const char **argv) {
int argc = 0;
while (argv[argc] != NULL) {
argc ++;
}
return sys_exec(name, argc, argv);
}

View File

@@ -0,0 +1,49 @@
#ifndef __USER_LIBS_ULIB_H__
#define __USER_LIBS_ULIB_H__
#include <defs.h>
void __warn(const char *file, int line, const char *fmt, ...);
void __noreturn __panic(const char *file, int line, const char *fmt, ...);
#define warn(...) \
__warn(__FILE__, __LINE__, __VA_ARGS__)
#define panic(...) \
__panic(__FILE__, __LINE__, __VA_ARGS__)
#define assert(x) \
do { \
if (!(x)) { \
panic("assertion failed: %s", #x); \
} \
} while (0)
// static_assert(x) will generate a compile-time error if 'x' is false.
#define static_assert(x) \
switch (x) { case 0: case (x): ; }
int fprintf(int fd, const char *fmt, ...);
void __noreturn exit(int error_code);
int fork(void);
int wait(void);
int waitpid(int pid, int *store);
void yield(void);
int kill(int pid);
int getpid(void);
void print_pgdir(void);
int sleep(unsigned int time);
unsigned int gettime_msec(void);
int __exec(const char *name, const char **argv);
#define __exec0(name, path, ...) \
({ const char *argv[] = {path, ##__VA_ARGS__, NULL}; __exec(name, argv); })
#define exec(path, ...) __exec0(NULL, path, ##__VA_ARGS__)
#define nexec(name, path, ...) __exec0(name, path, ##__VA_ARGS__)
void lab6_set_priority(uint32_t priority); //only for lab6
#endif /* !__USER_LIBS_ULIB_H__ */

View File

@@ -0,0 +1,34 @@
#include <ulib.h>
#include <unistd.h>
#include <file.h>
#include <stat.h>
int main(int argc, char *argv[]);
static int
initfd(int fd2, const char *path, uint32_t open_flags) {
int fd1, ret;
if ((fd1 = open(path, open_flags)) < 0) {
return fd1;
}
if (fd1 != fd2) {
close(fd2);
ret = dup2(fd1, fd2);
close(fd1);
}
return ret;
}
void
umain(int argc, char *argv[]) {
int fd;
if ((fd = initfd(0, "stdin:", O_RDONLY)) < 0) {
warn("open <stdin> failed: %e.\n", fd);
}
if ((fd = initfd(1, "stdout:", O_WRONLY)) < 0) {
warn("open <stdout> failed: %e.\n", fd);
}
int ret = main(argc, argv);
exit(ret);
}