From 67ee9510770674a4ef236168176bf85e0775ab7e Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Thu, 18 May 2017 15:33:37 +0200 Subject: added port types which are check when calling port read/write functions --- src/Makefile | 20 +++++++++++++------- src/README | 3 ++- src/interrupts.asm | 0 src/interrupts.c | 1 + src/interrupts.h | 4 ++++ src/kernel.c | 1 - src/port.asm | 35 ++++++++++++++++++++++++++++------- src/port.c | 3 ++- src/port.h | 18 +++++++++++++----- src/serial.c | 8 ++++---- src/serial.h | 4 ++-- src/stage1_functions.asm | 2 +- src/vga.c | 24 ++++++++++++------------ src/vga.h | 6 +++--- 14 files changed, 85 insertions(+), 44 deletions(-) create mode 100644 src/interrupts.asm create mode 100644 src/interrupts.c create mode 100644 src/interrupts.h diff --git a/src/Makefile b/src/Makefile index f22b5c0..affe3e3 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,7 @@ CC := gcc CFLAGS := -std=c99 -m32 -ffreestanding -O0 -g -Wall -Werror LD := ld -LDFLAGS := -f elf32 +NASMFLAGS := -f elf32 NASM := nasm OBJCOPY := objcopy @@ -14,8 +14,8 @@ image.bin: boot.bin kernel.bin magic.bin # + M * 512 (M is currently 7) = 3144 for kernel.bin # + 1 * 512 = 512 for magic.bin # (M + N + 1 is the number of sectors to be read in stage 2, as stage 1 - # loads only the first sector, so adapt NOF_LOAD_SECTORS to 12) - truncate -s 7168 image.tmp + # loads only the first sector, so adapt NOF_LOAD_SECTORS to 16) + truncate -s 8192 image.tmp cat image.tmp magic.bin > image.bin boot.bin: boot.asm gdt.asm stage1_functions.asm stage2_functions.asm switch_mode.asm @@ -27,10 +27,10 @@ kernel.bin: kernel.elf kernel.sym: kernel.elf $(OBJCOPY) --only-keep-debug kernel.elf kernel.sym -kernel.elf: kernel.o console.o vga.o serial.o port.o port_asm.o string.o stdlib.o +kernel.elf: kernel.o console.o vga.o serial.o port.o port_asm.o interrupts.o interrupts_asm.o string.o stdlib.o $(LD) -o kernel.elf -N -n -Ttext 0x8400 --oformat elf32-i386 \ kernel.o console.o vga.o serial.o port.o port_asm.o \ - string.o stdlib.o + interrupts.o interrupts_asm.o string.o stdlib.o magic.bin: magic.asm $(NASM) magic.asm -f bin -o magic.bin @@ -41,6 +41,9 @@ kernel.o: kernel.c port.o: port.c port.h $(CC) $(CFLAGS) -c -o port.o port.c +port_asm.o: port.asm + $(NASM) port.asm $(NASMFLAGS) -o port_asm.o + console.o: console.c console.h vga.h serial.h $(CC) $(CFLAGS) -c -o console.o console.c @@ -50,8 +53,11 @@ vga.o: vga.c vga.h serial.o: serial.c serial.h $(CC) $(CFLAGS) -c -o serial.o serial.c -port_asm.o: port.asm - $(NASM) port.asm $(LDFLAGS) -o port_asm.o +interrupts.o: interrupts.c interrupts.h + $(CC) $(CFLAGS) -c -o interrupts.o interrupts.c + +interrupts_asm.o: interrupts.asm + $(NASM) interrupts.asm $(NASMFLAGS) -o interrupts_asm.o string.o: string.c string.h $(CC) $(CFLAGS) -c -o string.o string.c diff --git a/src/README b/src/README index bfd0795..ad6ca2a 100644 --- a/src/README +++ b/src/README @@ -1,7 +1,7 @@ * boot.bin - boot sector (stage 1 and 2, total 2k), offset 0x7c00 * boot.asm - the main boot sector code using: * kernel.bin - linked kernel with fix start offset 0x8400 - * gdt.asm - the early GDT + * gdt.asm - the early GDT, flat memory model, no protection * stage1_functions.asm - real mode functions of the bootloader * stage2_functions.asm - protected mode primitive VGA routines * switch_mode.asm - early GTD loading and switching from real to protected mode @@ -14,6 +14,7 @@ kernel utility routines * vga.c - VGA basic output routines for early kernel output * serial.c - serial output to COM1 (only sequential ASCII chars, no terminal) * port.c, port.asm - I/O ports +* interrupts.c, interrups.asm - interrupt handlers C library routines * string.c diff --git a/src/interrupts.asm b/src/interrupts.asm new file mode 100644 index 0000000..e69de29 diff --git a/src/interrupts.c b/src/interrupts.c new file mode 100644 index 0000000..e04d063 --- /dev/null +++ b/src/interrupts.c @@ -0,0 +1 @@ +#include "interrupts.h" diff --git a/src/interrupts.h b/src/interrupts.h new file mode 100644 index 0000000..53a1a60 --- /dev/null +++ b/src/interrupts.h @@ -0,0 +1,4 @@ +#ifndef INTERRUPTS_H +#define INTERRUPTS_H + +#endif // INTERRUPTS_H diff --git a/src/kernel.c b/src/kernel.c index 69a3870..6408aa7 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -48,7 +48,6 @@ void entry( void ) console_put_string( &console, "Terminating" ); console_put_newline( &console ); - //~ vga_set_color( &vga, VGA_COLOR_WHITE ); //~ vga_set_background_color( &vga, VGA_COLOR_RED ); //~ vga_clear_screen( &vga ); diff --git a/src/port.asm b/src/port.asm index 4f2b646..f1bc76b 100644 --- a/src/port.asm +++ b/src/port.asm @@ -1,25 +1,46 @@ [bits 32] -global port8_write -global port8_read +global port_write8 +global port_read8 -; void port8_write( port8_t *port, uint8_t data ); -port8_write: +; typedef enum port_type_t { +; PORT_TYPE_8BIT = 0, +; PORT_TYPE_8BIT_SLOW = 1, +; PORT_TYPE_16BIT = 2, +; PORT_TYPE_32BIT = 3 +; } port_type_t; +PORT_TYPE_8BIT equ 0 +PORT_TYPE_8BIT_SLOW equ 1 +PORT_TYPE_16BIT equ 2 +PORT_TYPE_32BIT equ 3 + +; void port_write8( port_t *port, uint8_t data ); +port_write8: push ebp mov ebp, esp mov ecx, [ebp+8] mov edx, DWORD [ecx] + cmp edx, PORT_TYPE_8BIT + je .ok + int 10 +.ok: + mov edx, DWORD [ecx+4] movzx ax, BYTE [ebp+12] out dx, al leave ret -; uint8_t port8_read( port8_t *port ) -port8_read: +; uint8_t port_read8( port_t *port ) +port_read8: push ebp mov ebp, esp mov ecx, DWORD [ebp+8] - mov edx, [ecx] + mov edx, DWORD [ecx] + cmp edx, PORT_TYPE_8BIT + je .ok + int 11 +.ok: + mov edx, [ecx+4] in al, dx leave ret diff --git a/src/port.c b/src/port.c index f64cc69..8107936 100644 --- a/src/port.c +++ b/src/port.c @@ -1,6 +1,7 @@ #include "port.h" -void port8_init( port8_t *port, uint16_t number ) +void port_init( port_t *port, port_type_t type, uint16_t number ) { port->number = number; + port->type = type; } diff --git a/src/port.h b/src/port.h index a5e8bd8..28d07b7 100644 --- a/src/port.h +++ b/src/port.h @@ -3,12 +3,20 @@ #include -typedef struct port8_t { +typedef enum port_type_t { + PORT_TYPE_8BIT = 0, + PORT_TYPE_8BIT_SLOW = 1, + PORT_TYPE_16BIT = 2, + PORT_TYPE_32BIT = 3 +} port_type_t; + +typedef struct port_t { + port_type_t type; uint16_t number; // port number, e.g. 0x3d4 VGA index register -} port8_t; +} port_t; -void port8_init( port8_t *port, uint16_t number ); -void port8_write( port8_t *port, uint8_t data ); -uint8_t port8_read( port8_t *port ); +void port_init( port_t *port, port_type_t type, uint16_t number ); +void port_write8( port_t *port, uint8_t data ); +uint8_t port_read8( port_t *port ); #endif // diff --git a/src/serial.c b/src/serial.c index 0ca3063..409b024 100644 --- a/src/serial.c +++ b/src/serial.c @@ -6,18 +6,18 @@ void serial_init( serial_t *serial ) { memset( serial, 0, sizeof( serial_t ) ); - port8_init( &serial->port_3F8, 0x3F8 ); - port8_init( &serial->port_3FD, 0x3FD ); + port_init( &serial->port_3F8, PORT_TYPE_8BIT, 0x3F8 ); + port_init( &serial->port_3FD, PORT_TYPE_8BIT, 0x3FD ); } void serial_put_char( serial_t *serial, const char c ) { uint8_t status; do { - status = port8_read( &serial->port_3FD ); + status = port_read8( &serial->port_3FD ); } while( ( status & 0x20 ) == 0 ); - port8_write( &serial->port_3F8, c ); + port_write8( &serial->port_3F8, c ); } void serial_put_string( serial_t *serial, const char *s ) diff --git a/src/serial.h b/src/serial.h index f50a62d..81c1e83 100644 --- a/src/serial.h +++ b/src/serial.h @@ -4,8 +4,8 @@ #include "port.h" typedef struct serial_t { - port8_t port_3F8; - port8_t port_3FD; + port_t port_3F8; + port_t port_3FD; } serial_t; void serial_init( serial_t *serial ); diff --git a/src/stage1_functions.asm b/src/stage1_functions.asm index 6a145f6..440b4be 100644 --- a/src/stage1_functions.asm +++ b/src/stage1_functions.asm @@ -2,7 +2,7 @@ ; 3 * 512 for bootloader stage2 and the kernel code ; (note: the first sector gets loaded by the BIOS, so ; subtract 1 here!) -NOF_LOAD_SECTORS equ 14 +NOF_LOAD_SECTORS equ 16 ; IN bx: begin of memory area to dump ; IN ax: number of words to dump diff --git a/src/vga.c b/src/vga.c index aba3564..609b831 100644 --- a/src/vga.c +++ b/src/vga.c @@ -21,12 +21,12 @@ void vga_init( vga_t *vga ) // make sure we use the 0x3dx VGA ports, is done // in assembly in stage 2 too - port8_init( &vga->crtc_misc_port, 0x3d2 ); - port8_write( &vga->crtc_misc_port, 1 ); + port_init( &vga->crtc_misc_port, PORT_TYPE_8BIT, 0x3d2 ); + port_write8( &vga->crtc_misc_port, 1 ); // set up VGA ports - port8_init( &vga->crtc_index_port, 0x3d4 ); - port8_init( &vga->crtc_data_port, 0x3d5 ); + port_init( &vga->crtc_index_port, PORT_TYPE_8BIT, 0x3d4 ); + port_init( &vga->crtc_data_port, PORT_TYPE_8BIT, 0x3d5 ); vga_set_cursor_from_hardware( vga ); }; @@ -59,12 +59,12 @@ void vga_set_cursor_from_hardware( vga_t *vga ) { uint16_t hw_cursor_pos; - port8_write( &vga->crtc_index_port, 14 ); - uint8_t data = port8_read( &(vga->crtc_data_port ) ); + port_write8( &vga->crtc_index_port, 14 ); + uint8_t data = port_read8( &(vga->crtc_data_port ) ); hw_cursor_pos = data << 8; - port8_write( &vga->crtc_index_port, 15 ); - data = port8_read( &(vga->crtc_data_port ) ); + port_write8( &vga->crtc_index_port, 15 ); + data = port_read8( &(vga->crtc_data_port ) ); hw_cursor_pos |= data; @@ -83,10 +83,10 @@ void vga_set_cursor_on_hardware( vga_t *vga ) { uint16_t hw_cursor_pos = vga->cursor_x + vga->cursor_y * vga->res_x; - port8_write( &vga->crtc_index_port, 15 ); - port8_write( &vga->crtc_data_port, hw_cursor_pos & 0xff ); - port8_write( &vga->crtc_index_port, 14 ); - port8_write( &vga->crtc_data_port, hw_cursor_pos >> 8 ); + port_write8( &vga->crtc_index_port, 15 ); + port_write8( &vga->crtc_data_port, hw_cursor_pos & 0xff ); + port_write8( &vga->crtc_index_port, 14 ); + port_write8( &vga->crtc_data_port, hw_cursor_pos >> 8 ); } int vga_get_cursor_x( vga_t *vga ) diff --git a/src/vga.h b/src/vga.h index 3a91154..bc2449a 100644 --- a/src/vga.h +++ b/src/vga.h @@ -34,9 +34,9 @@ typedef struct vga_t { int cursor_y; // current cursor position Y vga_color_t color; vga_color_t background_color; - port8_t crtc_misc_port; - port8_t crtc_index_port; - port8_t crtc_data_port; + port_t crtc_misc_port; + port_t crtc_index_port; + port_t crtc_data_port; } vga_t; void vga_init( vga_t *vga ); -- cgit v1.2.3-54-g00ecf