实现用户程序退出处理

This commit is contained in:
XIAOYI12 2024-08-31 12:36:49 +08:00
parent be9c0b187c
commit 80570dcdbf
15 changed files with 485 additions and 57 deletions

View File

@ -10,7 +10,7 @@ NASM = nasm
LIBS_PATH := ../libo
LD = ld
LD_FLAGS = -Ttext 0xb0000010 -m elf_i386 -static -e main
LD_FLAGS = -Ttext 0xb0000010 -m elf_i386 -static -e _start
LINK = $(LD) $(LD_FLAGS)
BASIC_LIB_C = $(LIBS_PATH)/libp.a

4
apps/include/libc.h Normal file
View File

@ -0,0 +1,4 @@
#ifndef CRASHPOWEROS_LIBC_H
#define CRASHPOWEROS_LIBC_H
#endif

View File

@ -3,7 +3,11 @@
#include <stdint.h>
#include <stddef.h>
#include <stdarg.h>
void put_char(char a);
int printf(const char* fmt, ...);
int puts(const char *s);
int vsprintf(char *buf, const char *fmt, va_list args);
#endif

16
apps/include/string.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef CRASHPOWEROS_STRING_H
#define CRASHPOWEROS_STRING_H
#include <stddef.h>
int isspace(int c);
int isdigit(int c);
int isalpha(int c);
int isupper(int c);
size_t strnlen(const char *s, size_t maxlen);
size_t strlen(const char *str);
int strcmp(const char *s1, const char *s2);
char *strcpy(char *dest, const char *src);
char *strcat(char *dest, const char *src);
#endif

View File

@ -1,30 +1,21 @@
#ifndef CRASHPOWEROS_SYSCALL_H
#define CRASHPOWEROS_SYSCALL_H
#define SYSCALL_PUTCHAR 1
#define SYSCALL_PUTC 1
#define SYSCALL_PRINT 2
#define SYSCALL_GETC 3
#define SYSCALL_MALLOC 4
#define SYSCALL_FREE 5
#define SYSCALL_EXIT 6
#include <stdint.h>
#include <stddef.h>
static inline void syscall_print(char* c){
uint32_t rets;
uint32_t __arg1 = c;
register uint32_t ebx asm("ebx") = __arg1;
asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_PRINT), "r"(ebx) : "memory", "cc");
}
static inline void syscall_putchar(char c){
uint32_t rets;
uint32_t __arg1 = (uint32_t)(c);
register uint32_t ebx asm("ebx") = __arg1;
asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_PUTCHAR), "r"(ebx) : "memory", "cc");
}
static inline char syscall_getc(){
uint32_t rets;
asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_GETC) : "memory", "cc");
return rets;
}
void syscall_print(char* c);
void syscall_putchar(char c);
char syscall_getc();
void* syscall_malloc(size_t size);
void syscall_free(void *ptr);
void syscall_exit(int code);
#endif

View File

@ -1,34 +1,10 @@
#include "../include/syscall.h"
int gets(char *buf) {
int index = 0;
char c;
while ((c = syscall_getc()) != '\n') {
if (c == '\b') {
if (index > 0) {
index--;
syscall_print("\b \b");
}
} else {
buf[index++] = c;
syscall_putchar(c);
}
}
buf[index] = '\0';
syscall_putchar(c);
return index;
}
#include "../include/stdio.h"
void hlt(){
while (1);
}
int main(){
syscall_print("Debug info\n");
//char* info;
//gets(info);
//syscall_print(info);
//put_char('A');
hlt();
return 0;
printf("User application %d\n",12);
return -1;
}

View File

@ -1,4 +1,4 @@
OBJS_PACK = out/syscall.obj
OBJS_PACK = out/syscall.obj out/print.obj out/string.obj out/libc.obj
default : $(OBJS_PACK)

13
apps/libs/libc.c Normal file
View File

@ -0,0 +1,13 @@
#include "../include/stdio.h"
#include "../include/syscall.h"
void put_char(char c){
syscall_putchar(c);
}
void _start(){
extern int main();
int ret = main();
syscall_exit(ret);
while (1);
}

288
apps/libs/print.c Normal file
View File

@ -0,0 +1,288 @@
#include "../include/stdio.h"
#include "../include/syscall.h"
#include "../include/string.h"
static int skip_atoi(const char **s) {
int i = 0;
while (isdigit(**s))
i = i * 10 + *((*s)++) - '0';
return i;
}
#define ZEROPAD 1 /* pad with zero */
#define SIGN 2 /* unsigned/signed long */
#define PLUS 4 /* show plus */
#define SPACE 8 /* space if plus */
#define LEFT 16 /* left justified */
#define SMALL 32 /* Must be 32 == 0x20 */
#define SPECIAL 64 /* 0x */
#define __do_div(n, base) ({ \
int __res; \
__res = ((unsigned long) n) % (unsigned) base; \
n = ((unsigned long) n) / (unsigned) base; \
__res; })
int printf(const char* fmt, ...) {
static char buf[4096];
va_list va;
va_start(va, fmt);
int rets = vsprintf(buf, fmt, va);
va_end(va);
syscall_print(buf);
return rets;
}
int puts(const char *s) {
syscall_print(s);
syscall_print("\n");
return 0;
}
static char *number(char *str, long num, int base, int size, int precision,
int type) {
static const char digits[16] = "0123456789ABCDEF";
char tmp[66];
char c, sign, locase;
int i;
locase = (type & SMALL);
if (type & LEFT)
type &= ~ZEROPAD;
if (base < 2 || base > 16)
return NULL;
c = (type & ZEROPAD) ? '0' : ' ';
sign = 0;
if (type & SIGN) {
if (num < 0) {
sign = '-';
num = -num;
size--;
} else if (type & PLUS) {
sign = '+';
size--;
} else if (type & SPACE) {
sign = ' ';
size--;
}
}
if (type & SPECIAL) {
if (base == 16)
size -= 2;
else if (base == 8)
size--;
}
i = 0;
if (num == 0)
tmp[i++] = '0';
else
while (num != 0)
tmp[i++] = (digits[__do_div(num, base)] | locase);
if (i > precision)
precision = i;
size -= precision;
if (!(type & (ZEROPAD + LEFT)))
while (size-- > 0)
*str++ = ' ';
if (sign)
*str++ = sign;
if (type & SPECIAL) {
if (base == 8)
*str++ = '0';
else if (base == 16) {
*str++ = '0';
*str++ = ('X' | locase);
}
}
if (!(type & LEFT))
while (size-- > 0)
*str++ = c;
while (i < precision--)
*str++ = '0';
while (i-- > 0)
*str++ = tmp[i];
while (size-- > 0)
*str++ = ' ';
return str;
}
int vsprintf(char *buf, const char *fmt, va_list args) {
int len;
unsigned long num;
int i, base;
char *str;
const char *s;
int flags; /* flags to number() */
int field_width; /* width of output field */
int precision; /* min. # of digits for integers; max
number of chars for from string */
int qualifier; /* 'h', 'l', or 'L' for integer fields */
for (str = buf; *fmt; ++fmt) {
if (*fmt != '%') {
*str++ = *fmt;
continue;
}
/* process flags */
flags = 0;
repeat:
++fmt; /* this also skips first '%' */
switch (*fmt) {
case '-':
flags |= LEFT;
goto repeat;
case '+':
flags |= PLUS;
goto repeat;
case ' ':
flags |= SPACE;
goto repeat;
case '#':
flags |= SPECIAL;
goto repeat;
case '0':
flags |= ZEROPAD;
goto repeat;
}
/* get field width */
field_width = -1;
if (isdigit(*fmt))
field_width = skip_atoi(&fmt);
else if (*fmt == '*') {
++fmt;
/* it's the next argument */
field_width = va_arg(args,
int);
if (field_width < 0) {
field_width = -field_width;
flags |= LEFT;
}
}
/* get the precision */
precision = -1;
if (*fmt == '.') {
++fmt;
if (isdigit(*fmt))
precision = skip_atoi(&fmt);
else if (*fmt == '*') {
++fmt;
/* it's the next argument */
precision = va_arg(args,
int);
}
if (precision < 0)
precision = 0;
}
/* get the conversion qualifier */
qualifier = -1;
if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
qualifier = *fmt;
++fmt;
}
/* default base */
base = 10;
switch (*fmt) {
case 'c':
if (!(flags & LEFT))
while (--field_width > 0)
*str++ = ' ';
*str++ = (unsigned char) va_arg(args,
int);
while (--field_width > 0)
*str++ = ' ';
continue;
case 's':
s = va_arg(args,
char *);
len = strnlen(s, precision);
if (!(flags & LEFT))
while (len < field_width--)
*str++ = ' ';
for (i = 0; i < len; ++i)
*str++ = *s++;
while (len < field_width--)
*str++ = ' ';
continue;
case 'p':
if (field_width == -1) {
field_width = 2 * sizeof(void *);
flags |= ZEROPAD;
}
str = number(str, (unsigned long) va_arg(args,
void *), 16,
field_width, precision, flags);
continue;
case 'n':
if (qualifier == 'l') {
long *ip = va_arg(args,
long *);
*ip = (str - buf);
} else {
int *ip = va_arg(args,
int *);
*ip = (str - buf);
}
continue;
case '%':
*str++ = '%';
continue;
/* integer number formats - set up the flags and "break" */
case 'o':
base = 8;
break;
case 'x':
flags |= SMALL;
case 'X':
base = 16;
break;
case 'd':
case 'i':
flags |= SIGN;
case 'u':
break;
default:
*str++ = '%';
if (*fmt)
*str++ = *fmt;
else
--fmt;
continue;
}
if (qualifier == 'l')
num = va_arg(args,
unsigned long);
else if (qualifier == 'h') {
num = (unsigned short) va_arg(args,
int);
if (flags & SIGN)
num = (short) num;
} else if (flags & SIGN)
num = va_arg(args,
int);
else
num = va_arg(args,
unsigned int);
str = number(str, num, base, field_width, precision, flags);
}
*str = '\0';
return str - buf;
}

75
apps/libs/string.c Normal file
View File

@ -0,0 +1,75 @@
#include "../include/string.h"
int isspace(int c) {
return (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' ||
c == '\v');
}
// isdigit
int isdigit(int c) {
return (c >= '0' && c <= '9');
}
// isalpha
int isalpha(int c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
// isupper
int isupper(int c) {
return (c >= 'A' && c <= 'Z');
}
size_t strnlen(const char *s, size_t maxlen) {
const char *es = s;
while (*es && maxlen) {
es++;
maxlen--;
}
return (es - s);
}
size_t strlen(const char *str) {
size_t len = 0;
while (str[len])
len++;
return len;
}
int strcmp(const char *s1, const char *s2) {
char is_equal = 1;
for (; (*s1 != '\0') && (*s2 != '\0'); s1++, s2++) {
if (*s1 != *s2) {
is_equal = 0;
break;
}
}
if (is_equal) {
if (*s1 != '\0') {
return 1;
} else if (*s2 != '\0') {
return -1;
} else {
return 0;
}
} else {
return (int) (*s1 - *s2);
}
}
char *strcpy(char *dest, const char *src) {
do {
*dest++ = *src++;
} while (*src != 0);
*dest = 0;
}
char *strcat(char *dest, const char *src) {
char *temp = dest;
while (*temp != '\0')
temp++;
while ((*temp++ = *src++) != '\0');
}

View File

@ -1,2 +1,44 @@
#include "../include/syscall.h"
void syscall_print(char* c){
uint32_t rets;
uint32_t __arg1 = c;
register uint32_t ebx asm("ebx") = __arg1;
asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_PRINT), "r"(ebx) : "memory", "cc");
}
void syscall_putchar(char c){
uint32_t rets;
uint32_t __arg1 = (uint32_t)(c);
register uint32_t ebx asm("ebx") = __arg1;
asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_PUTC), "r"(ebx) : "memory", "cc");
}
char syscall_getc(){
uint32_t rets;
asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_GETC) : "memory", "cc");
return rets;
}
void* syscall_malloc(size_t size){
uint32_t rets;
uint32_t __arg1 = (uint32_t)(size);
register uint32_t ebx asm("ebx") = __arg1;
asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_MALLOC), "r"(ebx) : "memory", "cc");
return rets;
}
void syscall_free(void *ptr){
uint32_t rets;
uint32_t __arg1 = (uint32_t)(ptr);
register uint32_t ebx asm("ebx") = __arg1;
asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_FREE), "r"(ebx) : "memory", "cc");
}
void syscall_exit(int code){
uint32_t rets;
uint32_t __arg1 = (uint32_t)(code);
register uint32_t ebx asm("ebx") = __arg1;
asm volatile("int $31\n\t" : "=a"(rets) : "0"(SYSCALL_EXIT), "r"(ebx) : "memory", "cc");
return rets;
}

View File

@ -8,6 +8,9 @@
#define SYSCALL_PUTC 1
#define SYSCALL_PRINT 2
#define SYSCALL_GETC 3
#define SYSCALL_MALLOC 4
#define SYSCALL_FREE 5
#define SYSCALL_EXIT 6
void syscall_install();

View File

@ -5,6 +5,8 @@
#include "../include/graphics.h"
#include "../include/io.h"
#include "../include/shell.h"
#include "../include/heap.h"
static void syscall_puchar(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,uint32_t edi){
printf("%c",ebx);
@ -15,13 +17,33 @@ static void syscall_print(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,ui
}
static char syscall_getc(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,uint32_t edi){
return getc();
io_sti();
char c = getc();
printf("SYSCALL: %c\n",c);
return c;
}
static void syscall_exit(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,uint32_t edi){
task_kill(get_current()->pid);
printf("PID[%d] exit code: %d",get_current()->pid,ebx);
}
static void* syscall_malloc(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,uint32_t edi){
void* address = user_alloc(get_current(),ebx);
return address;
}
static void syscall_free(uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t esi,uint32_t edi){
use_free(get_current(),ebx);
}
void *sycall_handlers[MAX_SYSCALLS] = {
[SYSCALL_PUTC] = syscall_puchar,
[SYSCALL_PRINT] = syscall_print,
[SYSCALL_GETC] = syscall_getc,
[SYSCALL_MALLOC] = syscall_malloc,
[SYSCALL_FREE] = syscall_free,
[SYSCALL_EXIT] = syscall_exit,
};
typedef size_t (*syscall_t)(size_t, size_t, size_t, size_t, size_t);

View File

@ -145,8 +145,6 @@ void task_kill(int pid) {
}
argv->state = TASK_DEATH;
printf("Taskkill process PID:%d Name:%s\n", argv->pid, argv->name);
printf("Task [%s] exit code: -130.\n", argv->name);
kfree(argv);
struct task_struct *head = running_proc_head;
struct task_struct *last = NULL;

View File

@ -387,7 +387,6 @@ void setup_shell() {
char *buffer[255];
while (1) {
logk("DEBUG I\n");
if(hasFS) vfs_getPath(buffer);
else{
buffer[0] = 'n';
@ -397,11 +396,8 @@ void setup_shell() {
buffer[4] = 's';
buffer[5] = '\0';
}
logk("DEBUG II\n");
printf("\03343cd80;default@localhost: \0334169E1;%s\\\033c6c6c6;$ ", buffer);
logk("DEBUG III\n");
if (gets(com, MAX_COMMAND_LEN) <= 0) continue;
logk("DEBUG IIII\n");
argc = cmd_parse(com, argv, ' ');