add exercise of lab5 spoc discussion

This commit is contained in:
yuchen
2015-04-20 12:44:41 +08:00
parent 331b8dff5a
commit f39299c9a2
104 changed files with 12528 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
.text
.globl _start
_start:
# set ebp for backtrace
movl $0x0, %ebp
# move down the esp register
# since it may cause page fault in backtrace
subl $0x20, %esp
# call user-program function
call umain
1: jmp 1b

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,62 @@
#include <defs.h>
#include <stdio.h>
#include <syscall.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, &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;
}

View File

@@ -0,0 +1,72 @@
#include <defs.h>
#include <unistd.h>
#include <stdarg.h>
#include <syscall.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);
}

View File

@@ -0,0 +1,14 @@
#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_yield(void);
int sys_kill(int pid);
int sys_getpid(void);
int sys_putc(int c);
int sys_pgdir(void);
#endif /* !__USER_LIBS_SYSCALL_H__ */

View File

@@ -0,0 +1,48 @@
#include <defs.h>
#include <syscall.h>
#include <stdio.h>
#include <ulib.h>
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();
}

View File

@@ -0,0 +1,36 @@
#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): ; }
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);
#endif /* !__USER_LIBS_ULIB_H__ */

View File

@@ -0,0 +1,10 @@
#include <ulib.h>
int main(void);
void
umain(void) {
int ret = main();
exit(ret);
}