From 3ed830019561a06b0cb98f792cc447a157fd4ef2 Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Mon, 6 Aug 2018 00:32:06 +1000 Subject: Defined ssize_t in unistd.h. Added the _syscall2 macro to unistd.h to facilitate system calls that require two arguments to be passed. Modified the ATA driver to simply abort initialisation if a drive is not found, or cannot be configured. This will allow the kernel to function on a diskless system without invoking panic() unnecessarily. Added the functions irq_enable() and irq_disable() to asm/interrupt.h to make it easier for C code to mask and unmask IRQ's on each PIC. Moved the declaration for rsputs() from kernel/con.h to the new kernel/serial.h file since this is a function provided by the serial driver. Implemented a basic I/O input framework. This involves the new system call sys_read, which takes an I/O read request and directs it to the appropriate kernel handler function depending on the calling process' ctty value. This mechanism is identical to the sys_puts system call. Added the rsread() function to service sys_read calls from processes whose ctty value is equal to 1. This function will continually copy data from the serial buffer to the location specified. If there is not a sufficient amount of new data in the buffer to satisfy the request, the process is put into the TSTATE_UNINTERRUPTIBLE state and the scheduler is called to switch tasks. Prior to calling the scheduler, the function will set the waiting_task pointer to the calling process. This pointer will later be used by the interrupt handler to wake the process when new data arrives. Added an interrupt handler to service the IRQ4 (UART) interrupt. This subroutine is a stub which will save the machine's state then transfer control to rs_handler() in serial.c which will read bytes from the serial port and place them in a buffer. Before returning, rs_handler() checks the waiting_task pointer to see if a task is waiting for the newly received data and if so, it sets the task's state to TSTATE_RUNNING before resetting the pointer to NULL and returning. Ideally, the scheduler should be invoked at this point to select another task but since our basic round-robin scheduler currently has no concept of task priorities (and for the sake of simplicity), we will avoid invoking the scheduler in response to interrupts for now. --- include/asm/interrupt.h | 27 +++++++++++++++++++++++++++ include/kernel/con.h | 2 -- include/kernel/serial.h | 10 ++++++++++ include/kernel/sys.h | 3 ++- include/stdio.h | 4 ++++ include/unistd.h | 21 +++++++++++++++++++-- 6 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 include/kernel/serial.h (limited to 'include') diff --git a/include/asm/interrupt.h b/include/asm/interrupt.h index d1ea6c1..1187fef 100644 --- a/include/asm/interrupt.h +++ b/include/asm/interrupt.h @@ -1,2 +1,29 @@ +#include + +#define irq_enable(n) \ + __asm__ volatile ( \ + "inb $0xA1, %%al\n" \ + "shl $8, %%ax\n" \ + "inb $0x21, %%al\n" \ + "xor $0xFFFF, %%bx\n" \ + "and %%bx, %%ax\n" \ + "outb %%al, $0x21\n" \ + "shr $8, %%ax\n" \ + "outb %%al, $0xA1\n" \ + :: "b" (n) \ + ); + +#define irq_disable(n) \ + __asm__ volatile ( \ + "inb $0xA1, %%al\n" \ + "shl $8, %%ax\n" \ + "inb $0x21, %%al\n" \ + "or %%bx, %%ax\n" \ + "outb %%al, $0x21\n" \ + "shr $8, %%ax\n" \ + "outb %%al, $0xA1\n" \ + :: "b" (n) \ + ); + void register_isr(uint8_t n, uint8_t dpl, void *handler); void register_trap(uint8_t n, uint8_t dpl, void *handler); diff --git a/include/kernel/con.h b/include/kernel/con.h index d28daff..9dd9dbe 100644 --- a/include/kernel/con.h +++ b/include/kernel/con.h @@ -13,6 +13,4 @@ void con_clear(void); int printk(char*, ...); -int rsputs(char*); - #endif diff --git a/include/kernel/serial.h b/include/kernel/serial.h new file mode 100644 index 0000000..dbfa7f2 --- /dev/null +++ b/include/kernel/serial.h @@ -0,0 +1,10 @@ +#ifndef _SERIAL_H +#define _SERIAL_H + +#include + +int rsputs(char*); + +ssize_t rsread(void*, size_t); + +#endif diff --git a/include/kernel/sys.h b/include/kernel/sys.h index 32788b1..c428c72 100644 --- a/include/kernel/sys.h +++ b/include/kernel/sys.h @@ -8,6 +8,7 @@ extern int sys_signal(void); extern int sys_alarm(void); extern int sys_pause(void); extern int sys_ctty(void); +extern int sys_read(void); extern int sys_dummy(void); syscall_t call_table[256] = { @@ -19,7 +20,7 @@ syscall_t call_table[256] = { [5] = &sys_alarm, [6] = &sys_pause, [7] = &sys_ctty, - [8] = &sys_dummy, + [8] = &sys_read, [9] = &sys_dummy, [10] = &sys_dummy, [11] = &sys_dummy, diff --git a/include/stdio.h b/include/stdio.h index 824eb31..f94e783 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -2,6 +2,8 @@ #define _STDIO_H #include +#include +#include #define EOF -1 @@ -12,6 +14,8 @@ int sprintf(char*, char*, ...); int vsprintf(char*, char*, va_list); +ssize_t read(void *buf, size_t len); + int ctty(int ctty); #endif diff --git a/include/unistd.h b/include/unistd.h index 746c829..ef6d059 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -3,6 +3,11 @@ #include +#ifndef _SSIZE_T +#define _SSIZE_T +typedef int32_t ssize_t; +#endif + typedef uint16_t pid_t; #define __SYS_puts 0 @@ -13,6 +18,7 @@ typedef uint16_t pid_t; #define __SYS_alarm 5 #define __SYS_pause 6 #define __SYS_ctty 7 +#define __SYS_read 8 #define _syscall0(type, name) \ type name(void) { \ @@ -21,7 +27,7 @@ typedef uint16_t pid_t; "int $0x80" \ : "=a" (__res) \ : "a" (__SYS_##name) \ - :); \ + ); \ return __res; \ } @@ -32,7 +38,18 @@ typedef uint16_t pid_t; "int $0x80" \ : "=a" (__res) \ : "a" (__SYS_##name), "b" (a) \ - :); \ + ); \ + return __res; \ + } + +#define _syscall2(type, name, atype, a, btype, b) \ + type name(atype a, btype b) { \ + type __res; \ + __asm__ volatile ( \ + "int $0x80" \ + : "=a" (__res) \ + : "a" (__SYS_##name), "b" (a), "c" (b) \ + ); \ return __res; \ } -- cgit v1.3