From 544acef0986977ef9d3a05d87bb9f55163b1280a Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sun, 9 Aug 2020 17:27:08 +0200 Subject: Temporary cpu and serial drivers in libc --- apps/a.c | 34 ++---------------------- apps/b.c | 36 +++----------------------- apps/init.c | 35 +++---------------------- kernel/Makefile | 2 -- kernel/drivers/cpu.c | 69 ------------------------------------------------- kernel/drivers/serial.c | 32 ----------------------- kernel/inc/cpu.h | 29 --------------------- kernel/inc/serial.h | 9 ------- kernel/main.c | 5 ++-- lib/Makefile | 5 +++- lib/cpu.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/inc/cpu.h | 29 +++++++++++++++++++++ lib/inc/mem.h | 7 ++--- lib/inc/print.h | 1 + lib/inc/serial.h | 9 +++++++ lib/print.c | 10 +++++-- lib/serial.c | 32 +++++++++++++++++++++++ 17 files changed, 166 insertions(+), 247 deletions(-) delete mode 100644 kernel/drivers/cpu.c delete mode 100644 kernel/drivers/serial.c delete mode 100644 kernel/inc/cpu.h delete mode 100644 kernel/inc/serial.h create mode 100644 lib/cpu.c create mode 100644 lib/inc/cpu.h create mode 100644 lib/inc/serial.h create mode 100644 lib/serial.c diff --git a/apps/a.c b/apps/a.c index 45375c9..040a454 100644 --- a/apps/a.c +++ b/apps/a.c @@ -1,41 +1,11 @@ // MIT License, Copyright (c) 2020 Marvin Borner #include -#include - -u8 inb(u16 port) -{ - u8 value; - __asm__ volatile("inb %1, %0" : "=a"(value) : "Nd"(port)); - return value; -} - -void outb(u16 port, u8 data) -{ - __asm__ volatile("outb %0, %1" ::"a"(data), "Nd"(port)); -} - -int is_transmit_empty() -{ - return inb(0x3f8 + 5) & 0x20; -} - -void serial_put(char ch) -{ - while (is_transmit_empty() == 0) - ; - outb(0x3f8, (u8)ch); -} - -void serial_print(const char *data) -{ - for (u32 i = 0; i < strlen(data); i++) - serial_put(data[i]); -} +#include void main() { while (1) { - serial_print("a"); + print("a"); } } diff --git a/apps/b.c b/apps/b.c index 20ca888..1ed9249 100644 --- a/apps/b.c +++ b/apps/b.c @@ -1,42 +1,12 @@ // MIT License, Copyright (c) 2020 Marvin Borner #include -#include - -u8 inb(u16 port) -{ - u8 value; - __asm__ volatile("inb %1, %0" : "=a"(value) : "Nd"(port)); - return value; -} - -void outb(u16 port, u8 data) -{ - __asm__ volatile("outb %0, %1" ::"a"(data), "Nd"(port)); -} - -int is_transmit_empty() -{ - return inb(0x3f8 + 5) & 0x20; -} - -void serial_put(char ch) -{ - while (is_transmit_empty() == 0) - ; - outb(0x3f8, (u8)ch); -} - -void serial_print(const char *data) -{ - for (u32 i = 0; i < strlen(data); i++) - serial_put(data[i]); -} +#include void main() { - serial_print("\nB loaded\n"); + print("\nB loaded\n"); while (1) { - serial_print("b"); + print("b"); } } diff --git a/apps/init.c b/apps/init.c index c35f26f..91f5083 100644 --- a/apps/init.c +++ b/apps/init.c @@ -1,43 +1,14 @@ // MIT License, Copyright (c) 2020 Marvin Borner #include +#include #include -u8 inb(u16 port) -{ - u8 value; - __asm__ volatile("inb %1, %0" : "=a"(value) : "Nd"(port)); - return value; -} - -void outb(u16 port, u8 data) -{ - __asm__ volatile("outb %0, %1" ::"a"(data), "Nd"(port)); -} - -int is_transmit_empty() -{ - return inb(0x3f8 + 5) & 0x20; -} - -void serial_put(char ch) -{ - while (is_transmit_empty() == 0) - ; - outb(0x3f8, (u8)ch); -} - -void serial_print(const char *data) -{ - for (u32 i = 0; i < strlen(data); i++) - serial_put(data[i]); -} - void main() { - serial_print("Init loaded\n"); + print("Init loaded\n"); __asm__ volatile("int $0x80"); while (1) { - serial_print("b"); + print("b"); }; } diff --git a/kernel/Makefile b/kernel/Makefile index bbf13af..ed422a5 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -2,8 +2,6 @@ COBJS = main.o \ drivers/vesa.o \ - drivers/cpu.o \ - drivers/serial.o \ drivers/interrupts.o \ drivers/interrupts_asm.o \ drivers/keyboard.o \ diff --git a/kernel/drivers/cpu.c b/kernel/drivers/cpu.c deleted file mode 100644 index 5c27c51..0000000 --- a/kernel/drivers/cpu.c +++ /dev/null @@ -1,69 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner -// This file is a wrapper around some CPU asm calls - -#include - -u8 inb(u16 port) -{ - u8 value; - __asm__ volatile("inb %1, %0" : "=a"(value) : "Nd"(port)); - return value; -} - -u16 inw(u16 port) -{ - u16 value; - __asm__ volatile("inw %1, %0" : "=a"(value) : "Nd"(port)); - return value; -} - -u32 inl(u16 port) -{ - u32 value; - __asm__ volatile("inl %1, %0" : "=a"(value) : "Nd"(port)); - return value; -} - -void insl(u16 port, void *addr, int n) -{ - __asm__ volatile("cld; rep insl" - : "=D"(addr), "=c"(n) - : "d"(port), "0"(addr), "1"(n) - : "memory", "cc"); -} - -void outb(u16 port, u8 data) -{ - __asm__ volatile("outb %0, %1" ::"a"(data), "Nd"(port)); -} - -void outw(u16 port, u16 data) -{ - __asm__ volatile("outw %0, %1" ::"a"(data), "Nd"(port)); -} - -void outl(u16 port, u32 data) -{ - __asm__ volatile("outl %0, %1" ::"a"(data), "Nd"(port)); -} - -void cli() -{ - __asm__ volatile("cli"); -} - -void sti() -{ - __asm__ volatile("sti"); -} - -void hlt() -{ - __asm__ volatile("hlt"); -} - -void idle() -{ - while (1) - hlt(); -} diff --git a/kernel/drivers/serial.c b/kernel/drivers/serial.c deleted file mode 100644 index dcee4dd..0000000 --- a/kernel/drivers/serial.c +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include -#include - -void serial_install() -{ - outb(0x3f8 + 1, 0x00); - outb(0x3f8 + 3, 0x80); - outb(0x3f8 + 0, 0x03); - outb(0x3f8 + 1, 0x00); - outb(0x3f8 + 3, 0x03); - outb(0x3f8 + 2, 0xC7); - outb(0x3f8 + 4, 0x0B); -} - -int is_transmit_empty() -{ - return inb(0x3f8 + 5) & 0x20; -} - -void serial_put(char ch) -{ - while (is_transmit_empty() == 0) - ; - outb(0x3f8, (u8)ch); -} - -void serial_print(const char *data) -{ - for (u32 i = 0; i < strlen(data); i++) - serial_put(data[i]); -} diff --git a/kernel/inc/cpu.h b/kernel/inc/cpu.h deleted file mode 100644 index eb09291..0000000 --- a/kernel/inc/cpu.h +++ /dev/null @@ -1,29 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef CPU_H -#define CPU_H - -#include - -u8 inb(u16 port); -u16 inw(u16 port); -u32 inl(u16 port); -void insl(u16 port, void *addr, int n); - -void outb(u16 port, u8 data); -void outw(u16 port, u16 data); -void outl(u16 port, u32 data); -void cli(); -void sti(); -void hlt(); -void idle(); - -static inline void spinlock(int *ptr) -{ - int prev; - do - __asm__ volatile("lock xchgl %0,%1" : "=a"(prev) : "m"(*ptr), "a"(1)); - while (prev); -} - -#endif diff --git a/kernel/inc/serial.h b/kernel/inc/serial.h deleted file mode 100644 index 6511952..0000000 --- a/kernel/inc/serial.h +++ /dev/null @@ -1,9 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef SERIAL_H -#define SERIAL_H - -void serial_install(); -void serial_print(const char *data); - -#endif diff --git a/kernel/main.c b/kernel/main.c index 17d6267..85c1103 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -9,16 +9,15 @@ #include #include #include +#include #include #include #include #include -u32 HEAP = 0x00200000; -u32 HEAP_START; - void kernel_main(struct vid_info *vid_info) { + HEAP = 0x00200000; HEAP_START = HEAP; // For malloc function // Initialize VESA video diff --git a/lib/Makefile b/lib/Makefile index ad430d6..4190ec8 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -1,10 +1,13 @@ # MIT License, Copyright (c) 2020 Marvin Borner +# TODO: Remove serial and cpu from libc? COBJS = str.o \ mem.o \ math.o \ conv.o \ - print.o + print.o \ + serial.o \ + cpu.o CC = ../cross/opt/bin/i686-elf-gcc LD = ../cross/opt/bin/i686-elf-ld OC = ../cross/opt/bin/i686-elf-ar diff --git a/lib/cpu.c b/lib/cpu.c new file mode 100644 index 0000000..5c27c51 --- /dev/null +++ b/lib/cpu.c @@ -0,0 +1,69 @@ +// MIT License, Copyright (c) 2020 Marvin Borner +// This file is a wrapper around some CPU asm calls + +#include + +u8 inb(u16 port) +{ + u8 value; + __asm__ volatile("inb %1, %0" : "=a"(value) : "Nd"(port)); + return value; +} + +u16 inw(u16 port) +{ + u16 value; + __asm__ volatile("inw %1, %0" : "=a"(value) : "Nd"(port)); + return value; +} + +u32 inl(u16 port) +{ + u32 value; + __asm__ volatile("inl %1, %0" : "=a"(value) : "Nd"(port)); + return value; +} + +void insl(u16 port, void *addr, int n) +{ + __asm__ volatile("cld; rep insl" + : "=D"(addr), "=c"(n) + : "d"(port), "0"(addr), "1"(n) + : "memory", "cc"); +} + +void outb(u16 port, u8 data) +{ + __asm__ volatile("outb %0, %1" ::"a"(data), "Nd"(port)); +} + +void outw(u16 port, u16 data) +{ + __asm__ volatile("outw %0, %1" ::"a"(data), "Nd"(port)); +} + +void outl(u16 port, u32 data) +{ + __asm__ volatile("outl %0, %1" ::"a"(data), "Nd"(port)); +} + +void cli() +{ + __asm__ volatile("cli"); +} + +void sti() +{ + __asm__ volatile("sti"); +} + +void hlt() +{ + __asm__ volatile("hlt"); +} + +void idle() +{ + while (1) + hlt(); +} diff --git a/lib/inc/cpu.h b/lib/inc/cpu.h new file mode 100644 index 0000000..eb09291 --- /dev/null +++ b/lib/inc/cpu.h @@ -0,0 +1,29 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef CPU_H +#define CPU_H + +#include + +u8 inb(u16 port); +u16 inw(u16 port); +u32 inl(u16 port); +void insl(u16 port, void *addr, int n); + +void outb(u16 port, u8 data); +void outw(u16 port, u16 data); +void outl(u16 port, u32 data); +void cli(); +void sti(); +void hlt(); +void idle(); + +static inline void spinlock(int *ptr) +{ + int prev; + do + __asm__ volatile("lock xchgl %0,%1" : "=a"(prev) : "m"(*ptr), "a"(1)); + while (prev); +} + +#endif diff --git a/lib/inc/mem.h b/lib/inc/mem.h index e700e42..e2d574f 100644 --- a/lib/inc/mem.h +++ b/lib/inc/mem.h @@ -5,12 +5,13 @@ #include -u32 HEAP; -u32 HEAP_START; - #define malloc(n) ((void *)((HEAP += n) - n)) // TODO: Implement real/better malloc/free #define free(x) +// TODO: Use malloc as syscall +u32 HEAP; +u32 HEAP_START; + void *memcpy(void *dst, const void *src, u32 n); void *memset(void *dst, int c, u32 n); int memcmp(const void *s1, const void *s2, u32 n); diff --git a/lib/inc/print.h b/lib/inc/print.h index 925a5bd..04668b2 100644 --- a/lib/inc/print.h +++ b/lib/inc/print.h @@ -9,5 +9,6 @@ int printf(const char *format, ...); int vprintf(const char *format, va_list ap); int vsprintf(char *str, const char *format, va_list ap); +int print(const char *str); #endif diff --git a/lib/inc/serial.h b/lib/inc/serial.h new file mode 100644 index 0000000..6511952 --- /dev/null +++ b/lib/inc/serial.h @@ -0,0 +1,9 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef SERIAL_H +#define SERIAL_H + +void serial_install(); +void serial_print(const char *data); + +#endif diff --git a/lib/print.c b/lib/print.c index 738e42f..1cbed69 100644 --- a/lib/print.c +++ b/lib/print.c @@ -2,7 +2,7 @@ #include #include #include -/* #include */ +#include #include static void append(char *dest, char *src, int index) @@ -80,7 +80,7 @@ int vprintf(const char *format, va_list ap) char buf[1024]; memset(buf, 0, 1024); int len = vsprintf(buf, format, ap); - /* serial_print(buf); // TODO: Remove temporary serial print */ + serial_print(buf); // TODO: Remove temporary serial print return len; } @@ -94,3 +94,9 @@ int printf(const char *format, ...) return len; } + +int print(const char *str) +{ + serial_print(str); + return strlen(str); +} diff --git a/lib/serial.c b/lib/serial.c new file mode 100644 index 0000000..dcee4dd --- /dev/null +++ b/lib/serial.c @@ -0,0 +1,32 @@ +#include +#include +#include + +void serial_install() +{ + outb(0x3f8 + 1, 0x00); + outb(0x3f8 + 3, 0x80); + outb(0x3f8 + 0, 0x03); + outb(0x3f8 + 1, 0x00); + outb(0x3f8 + 3, 0x03); + outb(0x3f8 + 2, 0xC7); + outb(0x3f8 + 4, 0x0B); +} + +int is_transmit_empty() +{ + return inb(0x3f8 + 5) & 0x20; +} + +void serial_put(char ch) +{ + while (is_transmit_empty() == 0) + ; + outb(0x3f8, (u8)ch); +} + +void serial_print(const char *data) +{ + for (u32 i = 0; i < strlen(data); i++) + serial_put(data[i]); +} -- cgit v1.2.3