Skip to content

Commit

Permalink
✨ 064 printf
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenBaby committed Jun 29, 2022
1 parent bf76518 commit 2b5ccd4
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 4 deletions.
10 changes: 10 additions & 0 deletions docs/10 用户程序/064 printf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# printf

## 系统调用 write

```c++
int32 write(fd_t fd, char *buf, u32 len);
```
- kmalloc
- kfree
2 changes: 1 addition & 1 deletion src/include/onix/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

void console_init();
void console_clear();
void console_write(char *buf, u32 count);
int32 console_write(char *buf, u32 count);

#endif
1 change: 1 addition & 0 deletions src/include/onix/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

int vsprintf(char *buf, const char *fmt, va_list args);
int sprintf(char *buf, const char *fmt, ...);
int printf(const char *fmt, ...);

#endif
3 changes: 3 additions & 0 deletions src/include/onix/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
typedef enum syscall_t
{
SYS_NR_TEST,
SYS_NR_WRITE,
SYS_NR_SLEEP,
SYS_NR_YIELD,
} syscall_t;
Expand All @@ -14,4 +15,6 @@ u32 test();
void yield();
void sleep(u32 ms);

int32 write(fd_t fd, char *buf, u32 len);

#endif
8 changes: 8 additions & 0 deletions src/include/onix/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ typedef unsigned long long u64;
typedef u32 time_t;
typedef u32 idx_t;

typedef int32 fd_t;
typedef enum std_fd_t
{
stdin,
stdout,
stderr,
} std_fd_t;

#endif
6 changes: 4 additions & 2 deletions src/kernel/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,13 @@ static void command_del()

extern void start_beep();

void console_write(char *buf, u32 count)
int32 console_write(char *buf, u32 count)
{
bool intr = interrupt_disable(); // 禁止中断

char ch;
while (count--)
int32 nr = 0;
while (nr++ < count)
{
ch = *buf++;
switch (ch)
Expand Down Expand Up @@ -211,6 +212,7 @@ void console_write(char *buf, u32 count)

// 恢复中断
set_interrupt_state(intr);
return nr;
}

void console_init()
Expand Down
14 changes: 14 additions & 0 deletions src/kernel/gate.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <onix/debug.h>
#include <onix/syscall.h>
#include <onix/task.h>
#include <onix/console.h>

#define LOGK(fmt, args...) DEBUGK(fmt, ##args)

Expand Down Expand Up @@ -45,6 +46,17 @@ static u32 sys_test()
return 255;
}

int32 sys_write(fd_t fd, char *buf, u32 len)
{
if (fd == stdout || fd == stderr)
{
return console_write(buf, len);
}
// todo
panic("write!!!!");
return 0;
}

void syscall_init()
{
for (size_t i = 0; i < SYSCALL_SIZE; i++)
Expand All @@ -55,4 +67,6 @@ void syscall_init()
syscall_table[SYS_NR_TEST] = sys_test;
syscall_table[SYS_NR_SLEEP] = task_sleep;
syscall_table[SYS_NR_YIELD] = task_yield;

syscall_table[SYS_NR_WRITE] = sys_write;
}
4 changes: 3 additions & 1 deletion src/kernel/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <onix/syscall.h>
#include <onix/debug.h>
#include <onix/task.h>
#include <onix/stdio.h>

#define LOGK(fmt, args...) DEBUGK(fmt, ##args)

Expand Down Expand Up @@ -31,7 +32,8 @@ static void real_init_thread()
// asm volatile("in $0x92, %ax\n");
sleep(100);
// LOGK("%c\n", ch);
// printk("%c", ch);
// printk("task is in user mode %d\n", counter++);
printf("task is in user mode %d\n", counter++);
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/lib/printf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* (C) Copyright 2022 Steven;
* @author: Steven [email protected]
* @date: 2022-06-28
*/

#include <onix/stdarg.h>
#include <onix/stdio.h>
#include <onix/syscall.h>

static char buf[1024];

int printf(const char *fmt, ...)
{
va_list args;
int i;

va_start(args, fmt);

i = vsprintf(buf, fmt, args);

va_end(args);

write(stdout, buf, i);

return i;
}
25 changes: 25 additions & 0 deletions src/lib/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ static _inline u32 _syscall1(u32 nr, u32 arg)
return ret;
}

static _inline u32 _syscall2(u32 nr, u32 arg1, u32 arg2)
{
u32 ret;
asm volatile(
"int $0x80\n"
: "=a"(ret)
: "a"(nr), "b"(arg1), "c"(arg2));
return ret;
}

static _inline u32 _syscall3(u32 nr, u32 arg1, u32 arg2, u32 arg3)
{
u32 ret;
asm volatile(
"int $0x80\n"
: "=a"(ret)
: "a"(nr), "b"(arg1), "c"(arg2), "d"(arg3));
return ret;
}

u32 test()
{
return _syscall0(SYS_NR_TEST);
Expand All @@ -33,4 +53,9 @@ void yield()
void sleep(u32 ms)
{
_syscall1(SYS_NR_SLEEP, ms);
}

int32 write(fd_t fd, char *buf, u32 len)
{
return _syscall3(SYS_NR_WRITE, fd, (u32)buf, len);
}
1 change: 1 addition & 0 deletions src/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ $(BUILD)/kernel.bin: \
$(BUILD)/lib/vsprintf.o \
$(BUILD)/lib/stdlib.o \
$(BUILD)/lib/syscall.o \
$(BUILD)/lib/printf.o \

$(shell mkdir -p $(dir $@))
ld ${LDFLAGS} $^ -o $@
Expand Down

0 comments on commit 2b5ccd4

Please sign in to comment.