add lab answers
This commit is contained in:
14
labcodes_answer/lab7_result/user/libs/initcode.S
Normal file
14
labcodes_answer/lab7_result/user/libs/initcode.S
Normal 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
|
||||
|
||||
28
labcodes_answer/lab7_result/user/libs/panic.c
Normal file
28
labcodes_answer/lab7_result/user/libs/panic.c
Normal 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);
|
||||
}
|
||||
|
||||
62
labcodes_answer/lab7_result/user/libs/stdio.c
Normal file
62
labcodes_answer/lab7_result/user/libs/stdio.c
Normal 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;
|
||||
}
|
||||
|
||||
87
labcodes_answer/lab7_result/user/libs/syscall.c
Normal file
87
labcodes_answer/lab7_result/user/libs/syscall.c
Normal file
@@ -0,0 +1,87 @@
|
||||
#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);
|
||||
}
|
||||
|
||||
size_t
|
||||
sys_gettime(void) {
|
||||
return syscall(SYS_gettime);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
18
labcodes_answer/lab7_result/user/libs/syscall.h
Normal file
18
labcodes_answer/lab7_result/user/libs/syscall.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#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);
|
||||
/* FOR LAB6 ONLY */
|
||||
void sys_lab6_set_priority(uint32_t priority);
|
||||
|
||||
int sys_sleep(unsigned int time);
|
||||
|
||||
#endif /* !__USER_LIBS_SYSCALL_H__ */
|
||||
|
||||
63
labcodes_answer/lab7_result/user/libs/ulib.c
Normal file
63
labcodes_answer/lab7_result/user/libs/ulib.c
Normal file
@@ -0,0 +1,63 @@
|
||||
#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();
|
||||
}
|
||||
|
||||
unsigned int
|
||||
gettime_msec(void) {
|
||||
return (unsigned int)sys_gettime();
|
||||
}
|
||||
|
||||
void
|
||||
lab6_set_priority(uint32_t priority)
|
||||
{
|
||||
sys_lab6_set_priority(priority);
|
||||
}
|
||||
|
||||
int
|
||||
sleep(unsigned int time) {
|
||||
return sys_sleep(time);
|
||||
}
|
||||
39
labcodes_answer/lab7_result/user/libs/ulib.h
Normal file
39
labcodes_answer/lab7_result/user/libs/ulib.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#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);
|
||||
unsigned int gettime_msec(void);
|
||||
void lab6_set_priority(uint32_t priority);
|
||||
int sleep(unsigned int time);
|
||||
|
||||
#endif /* !__USER_LIBS_ULIB_H__ */
|
||||
|
||||
10
labcodes_answer/lab7_result/user/libs/umain.c
Normal file
10
labcodes_answer/lab7_result/user/libs/umain.c
Normal file
@@ -0,0 +1,10 @@
|
||||
#include <ulib.h>
|
||||
|
||||
int main(void);
|
||||
|
||||
void
|
||||
umain(void) {
|
||||
int ret = main();
|
||||
exit(ret);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user