diff options
74 files changed, 509 insertions, 67 deletions
@@ -13,21 +13,21 @@ clean: build: clean @set -e; \ echo "Building..."; \ - mkdir ./build/; \ + mkdir -p ./build/kernel && mkdir -p ./build/userspace; \ # Assemble ASM files - nasm -f elf ./src/kernel/boot.asm -o ./build/boot.o || exit; \ + nasm -f elf ./src/kernel/boot.asm -o ./build/kernel/boot.o || exit; \ - # Make all C files - find ./src/kernel/ ./src/mlibc/ -name \*.c >./build/tmp; \ + # Make all kernel C files + find ./src/kernel/ -name \*.c >./build/tmp; \ while read -r line; do \ stripped=$$(echo "$${line}" | sed -r 's/\//_/g'); \ stripped=$${stripped#??????}; \ stripped=$${stripped%%?}o; \ - i686-elf-gcc -c ./"$${line}" -o ./build/"$${stripped}" -I ./src -std=gnu99 -ffreestanding -O3 -Wall -Wextra -Wno-unused-parameter || exit; \ + i686-elf-gcc -c ./"$${line}" -o ./build/kernel/"$${stripped}" -I ./src -std=gnu99 -ffreestanding -O3 -Wall -Wextra -Wno-unused-parameter || exit; \ done <./build/tmp; \ rm ./build/tmp; \ - i686-elf-gcc -T ./src/kernel/linker.ld -I ./src -o ./build/melvix.bin -std=gnu99 -ffreestanding -O2 -nostdlib ./build/*.o || exit; \ + i686-elf-gcc -T ./src/kernel/linker.ld -I ./src -o ./build/melvix.bin -std=gnu99 -ffreestanding -O2 -nostdlib ./build/kernel/*.o || exit; \ # Modules i686-elf-gcc -c ./src/resources/font.c -o ./build/font.o -I ./src -std=gnu99 -ffreestanding -O2 -nostdlib; \ @@ -35,9 +35,16 @@ build: clean rm ./build/font.o; \ # Userspace - nasm -f elf ./src/userspace/start.asm -o ./build/user_start.o || exit; \ - i686-elf-gcc -c ./src/userspace/main.c -o ./build/user_main.o -I ./src/mlibc -I ./src/userspace -std=gnu99 -ffreestanding -O3 -Wall -Wextra -Wno-unused-parameter || exit; \ - i686-elf-gcc -I ./src/mlibc -I ./src/userspace -o ./build/user.o -std=gnu99 -ffreestanding -O2 -nostdlib ./build/user_start.o ./build/user_main.o || exit; \ + nasm -f elf ./src/userspace/start.asm -o ./build/userspace/start.o || exit; \ + find ./src/userspace/ -name \*.c >./build/tmp; \ + while read -r line; do \ + stripped=$$(echo "$${line}" | sed -r 's/\//_/g'); \ + stripped=$${stripped#??????}; \ + stripped=$${stripped%%?}o; \ + i686-elf-gcc -c ./"$${line}" -o ./build/userspace/"$${stripped}" -I ./src/userspace -std=gnu99 -ffreestanding -O3 -Wall -Wextra -Wno-unused-parameter || exit; \ + done <./build/tmp; \ + rm ./build/tmp; \ + i686-elf-gcc -I ./src/userspace -o ./build/user.o -std=gnu99 -ffreestanding -O2 -nostdlib ./build/userspace/*.o|| exit; \ i686-elf-objcopy -O binary ./build/user.o ./build/user.bin; \ # Create ISO diff --git a/src/kernel/cmos/rtc.c b/src/kernel/cmos/rtc.c index 0a67e64..0a5b63d 100644 --- a/src/kernel/cmos/rtc.c +++ b/src/kernel/cmos/rtc.c @@ -1,7 +1,7 @@ #include <kernel/io/io.h> #include <kernel/graphics/vesa.h> #include <kernel/acpi/acpi.h> -#include <mlibc/stdio.h> +#include <kernel/lib/stdio.h> unsigned char second; unsigned char minute; diff --git a/src/kernel/commands/command.c b/src/kernel/commands/command.c index c85356b..691684b 100644 --- a/src/kernel/commands/command.c +++ b/src/kernel/commands/command.c @@ -4,8 +4,8 @@ #include <kernel/graphics/vesa.h> #include <kernel/cmos/rtc.h> #include <kernel/timer/timer.h> -#include <mlibc/string.h> -#include <mlibc/stdio.h> +#include <kernel/lib/string.h> +#include <kernel/lib/stdio.h> int32_t starts_with(const char *a, const char *b) { diff --git a/src/kernel/fs/ata_pio.c b/src/kernel/fs/ata_pio.c index f7b78cc..2de57b8 100644 --- a/src/kernel/fs/ata_pio.c +++ b/src/kernel/fs/ata_pio.c @@ -1,5 +1,5 @@ #include <kernel/io/io.h> -#include <mlibc/stdlib.h> +#include <kernel/lib/stdlib.h> #include <kernel/fs/ata_pio.h> #include <kernel/interrupts/interrupts.h> diff --git a/src/kernel/fs/install.c b/src/kernel/fs/install.c index dfb6587..da0407d 100644 --- a/src/kernel/fs/install.c +++ b/src/kernel/fs/install.c @@ -3,7 +3,7 @@ #include <kernel/fs/marfs/marfs.h> #include <kernel/fs/iso9660/iso9660.h> #include <kernel/fs/atapi_pio.h> -#include <mlibc/stdlib.h> +#include <kernel/lib/stdlib.h> #include <kernel/acpi/acpi.h> #include <kernel/io/io.h> #include <kernel/timer/timer.h> diff --git a/src/kernel/fs/iso9660/iso9660.c b/src/kernel/fs/iso9660/iso9660.c index 81b1ba6..3c61d94 100644 --- a/src/kernel/fs/iso9660/iso9660.c +++ b/src/kernel/fs/iso9660/iso9660.c @@ -2,7 +2,7 @@ #include <kernel/lib/lib.h> #include <kernel/fs/atapi_pio.h> #include <kernel/fs/iso9660/iso9660.h> -#include <mlibc/stdlib.h> +#include <kernel/lib/stdlib.h> struct iso9660_entity *ISO9660_get(char **dirs, uint8_t dirs_sz) { diff --git a/src/kernel/fs/marfs/directory.c b/src/kernel/fs/marfs/directory.c index 210d908..d0a1bff 100644 --- a/src/kernel/fs/marfs/directory.c +++ b/src/kernel/fs/marfs/directory.c @@ -1,6 +1,6 @@ #include <stdint.h> #include <kernel/fs/ata_pio.h> -#include <mlibc/stdlib.h> +#include <kernel/lib/stdlib.h> #include <kernel/fs/marfs/marfs.h> uint32_t marfs_new_dir(uint32_t uid) diff --git a/src/kernel/fs/marfs/disklevel.c b/src/kernel/fs/marfs/disklevel.c index a902362..106540e 100644 --- a/src/kernel/fs/marfs/disklevel.c +++ b/src/kernel/fs/marfs/disklevel.c @@ -14,7 +14,7 @@ void marfs_format(void) sb.s_first_chunk = 2 + sb.n_inodes; // Write to disk - marfs_writeSB(&sb); + marfs_write_superblock(&sb); // Initialize the inodes for (uint32_t i = 0; i < sb.n_inodes; i++) ata_clear28(interface, 2 + i); diff --git a/src/kernel/fs/marfs/marfs.h b/src/kernel/fs/marfs/marfs.h index f197e5b..67b693c 100644 --- a/src/kernel/fs/marfs/marfs.h +++ b/src/kernel/fs/marfs/marfs.h @@ -42,7 +42,7 @@ uint8_t marfs_write_mbr(uint8_t *mbr); struct marfs_superblock *marfs_read_superblock(); -uint8_t marfs_writeSB(struct marfs_superblock *sb); +uint8_t marfs_write_superblock(struct marfs_superblock *sb); uint32_t marfs_get_free_lba_block(void); diff --git a/src/kernel/fs/marfs/new_file.c b/src/kernel/fs/marfs/new_file.c index 4503ed1..4fef3b9 100644 --- a/src/kernel/fs/marfs/new_file.c +++ b/src/kernel/fs/marfs/new_file.c @@ -1,6 +1,6 @@ #include <stdint.h> #include <kernel/fs/ata_pio.h> -#include <mlibc/stdlib.h> +#include <kernel/lib/stdlib.h> #include <kernel/fs/marfs/marfs.h> static uint8_t last_max_level = 0; diff --git a/src/kernel/fs/marfs/read_whole_file.c b/src/kernel/fs/marfs/read_whole_file.c index 324cd9b..85f9bbb 100644 --- a/src/kernel/fs/marfs/read_whole_file.c +++ b/src/kernel/fs/marfs/read_whole_file.c @@ -1,6 +1,6 @@ #include <stdint.h> #include <kernel/fs/ata_pio.h> -#include <mlibc/stdlib.h> +#include <kernel/lib/stdlib.h> #include <kernel/fs/marfs/marfs.h> static uint8_t last_max_level = 0; diff --git a/src/kernel/fs/marfs/sectorlevel.c b/src/kernel/fs/marfs/sectorlevel.c index d9fa2c4..d905315 100644 --- a/src/kernel/fs/marfs/sectorlevel.c +++ b/src/kernel/fs/marfs/sectorlevel.c @@ -1,5 +1,5 @@ #include <stdint.h> -#include <mlibc/stdlib.h> +#include <kernel/lib/stdlib.h> #include <kernel/fs/ata_pio.h> #include <kernel/fs/marfs/marfs.h> @@ -29,7 +29,7 @@ struct marfs_superblock *marfs_read_superblock() return p; } -uint8_t marfs_writeSB(struct marfs_superblock *sb) +uint8_t marfs_write_superblock(struct marfs_superblock *sb) { sb_cache = *sb; return ata_write28(interface, 1, (uint8_t *) sb); diff --git a/src/kernel/gdt/gdt.c b/src/kernel/gdt/gdt.c index 94ce06b..41e6909 100644 --- a/src/kernel/gdt/gdt.c +++ b/src/kernel/gdt/gdt.c @@ -2,7 +2,7 @@ #include <kernel/gdt/gdt.h> #include <kernel/system.h> #include <kernel/lib/lib.h> -#include <mlibc/stdlib/liballoc.h> +#include <kernel/lib/stdlib/liballoc.h> struct gdt_entry { unsigned short limit_low; diff --git a/src/kernel/graphics/font.c b/src/kernel/graphics/font.c index 458669b..147eaff 100644 --- a/src/kernel/graphics/font.c +++ b/src/kernel/graphics/font.c @@ -2,7 +2,7 @@ #include <kernel/paging/paging.h> #include <kernel/io/io.h> #include <kernel/graphics/font.h> -#include <mlibc/stdlib/liballoc.h> +#include <kernel/lib/stdlib/liballoc.h> #include <kernel/fs/ata_pio.h> #include <kernel/fs/atapi_pio.h> #include <kernel/system.h> diff --git a/src/kernel/graphics/vesa.c b/src/kernel/graphics/vesa.c index 4d6dc84..1b759e4 100644 --- a/src/kernel/graphics/vesa.c +++ b/src/kernel/graphics/vesa.c @@ -4,9 +4,9 @@ #include <kernel/lib/lib.h> #include <kernel/paging/paging.h> #include <kernel/system.h> -#include <mlibc/stdlib.h> +#include <kernel/lib/stdlib.h> #include <kernel/commands/command.h> -#include <mlibc/stdio.h> +#include <kernel/lib/stdio.h> void switch_to_vga() { diff --git a/src/kernel/input/input.h b/src/kernel/input/input.h index 063eee2..3152622 100644 --- a/src/kernel/input/input.h +++ b/src/kernel/input/input.h @@ -12,4 +12,8 @@ void mouse_install(); */ void keyboard_install(); +void keyboard_clear_buffer(); + +char *keyboard_buffer; + #endif diff --git a/src/kernel/input/ps2/keyboard.c b/src/kernel/input/ps2/keyboard.c index 7294349..9e133ef 100644 --- a/src/kernel/input/ps2/keyboard.c +++ b/src/kernel/input/ps2/keyboard.c @@ -1,6 +1,11 @@ #include <kernel/interrupts/interrupts.h> #include <kernel/io/io.h> #include <kernel/graphics/vesa.h> +#include <kernel/input/input.h> +#include <kernel/lib/stdlib/liballoc.h> +#include <kernel/lib/string.h> +#include <kernel/paging/paging.h> +#include <kernel/lib/lib.h> int shift_pressed; int control_pressed; @@ -101,7 +106,7 @@ void keyboard_handler(struct regs *r) return; } - vesa_keyboard_char(current_keymap[scan_code]); + keyboard_buffer[strlen(keyboard_buffer)] = current_keymap[scan_code]; } else { // RELEASE if (current_keymap[scan_code] == -107) // TODO: IDK WHY -107?! control_pressed = 0; @@ -120,9 +125,17 @@ void keyboard_rate() outb(0x60, 0x0); // Rate{00000} Delay{00} 0 } +void keyboard_clear_buffer() +{ + memset(keyboard_buffer, 0, 4096); +} + // Installs the keyboard handler into IRQ1 void keyboard_install() { + keyboard_buffer = (char *) paging_alloc_pages(1); // 4KiB + paging_set_user((uint32_t) keyboard_buffer, 1); + keyboard_rate(); irq_install_handler(1, keyboard_handler); shift_pressed = 0; diff --git a/src/kernel/input/ps2/mouse.c b/src/kernel/input/ps2/mouse.c index cb7406f..75d80a1 100644 --- a/src/kernel/input/ps2/mouse.c +++ b/src/kernel/input/ps2/mouse.c @@ -150,4 +150,4 @@ void mouse_install() // Setup the mouse handler irq_install_handler(12, mouse_handler); info("Installed mouse handler"); -} +}
\ No newline at end of file diff --git a/src/kernel/interrupts/isr.c b/src/kernel/interrupts/isr.c index 7a4d3f4..3dfd24c 100644 --- a/src/kernel/interrupts/isr.c +++ b/src/kernel/interrupts/isr.c @@ -3,7 +3,7 @@ #include <kernel/lib/lib.h> #include <kernel/system.h> #include <kernel/io/io.h> -#include <mlibc/string.h> +#include <kernel/lib/string.h> // Install ISRs in IDT void isrs_install() diff --git a/src/kernel/io/io.c b/src/kernel/io/io.c index c5ae657..6bd87e0 100644 --- a/src/kernel/io/io.c +++ b/src/kernel/io/io.c @@ -2,8 +2,8 @@ #include <kernel/lib/lib.h> #include <kernel/io/io.h> #include <kernel/system.h> -#include <mlibc/string.h> -#include <mlibc/stdlib.h> +#include <kernel/lib/string.h> +#include <kernel/lib/stdlib.h> uint8_t inb(uint16_t port) { diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 69ad72a..bfd0803 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -13,7 +13,7 @@ #include <kernel/fs/marfs/marfs.h> #include <kernel/fs/iso9660/iso9660.h> #include <kernel/fs/atapi_pio.h> -#include <mlibc/stdlib/liballoc.h> +#include <kernel/lib/stdlib/liballoc.h> extern void jump_userspace(); diff --git a/src/mlibc/math.h b/src/kernel/lib/math.h index c9abf7d..c9abf7d 100644 --- a/src/mlibc/math.h +++ b/src/kernel/lib/math.h diff --git a/src/mlibc/math/pow.c b/src/kernel/lib/math/pow.c index 24670a0..24670a0 100644 --- a/src/mlibc/math/pow.c +++ b/src/kernel/lib/math/pow.c diff --git a/src/mlibc/stdio.h b/src/kernel/lib/stdio.h index 82f1cfb..d102dd6 100644 --- a/src/mlibc/stdio.h +++ b/src/kernel/lib/stdio.h @@ -1,10 +1,12 @@ #ifndef MELVIX_STDIO_H #define MELVIX_STDIO_H -// TODO: Input methods - #include <stdarg.h> +char getch(); + +char *readline(); + void writec(char c); void vprintf(const char *format, va_list args); diff --git a/src/kernel/lib/stdio/getch.c b/src/kernel/lib/stdio/getch.c new file mode 100644 index 0000000..5361c97 --- /dev/null +++ b/src/kernel/lib/stdio/getch.c @@ -0,0 +1,9 @@ +#include <kernel/input/input.h> +#include <kernel/lib/string.h> + +char getch() +{ + keyboard_clear_buffer(); + while (strlen(keyboard_buffer) == 0) {} + return keyboard_buffer[0]; +}
\ No newline at end of file diff --git a/src/kernel/lib/stdio/printf.c b/src/kernel/lib/stdio/printf.c new file mode 100644 index 0000000..accb97d --- /dev/null +++ b/src/kernel/lib/stdio/printf.c @@ -0,0 +1,10 @@ +#include <stdarg.h> +#include <kernel/lib/stdio.h> + +void printf(const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); +}
\ No newline at end of file diff --git a/src/kernel/lib/stdio/readline.c b/src/kernel/lib/stdio/readline.c new file mode 100644 index 0000000..c65882d --- /dev/null +++ b/src/kernel/lib/stdio/readline.c @@ -0,0 +1,9 @@ +#include <kernel/input/input.h> +#include <kernel/lib/string.h> + +char *readline() +{ + keyboard_clear_buffer(); + while (keyboard_buffer[strlen(keyboard_buffer) - 1] != '\n') {} + return keyboard_buffer; +}
\ No newline at end of file diff --git a/src/kernel/lib/stdio/vprintf.c b/src/kernel/lib/stdio/vprintf.c new file mode 100644 index 0000000..885f584 --- /dev/null +++ b/src/kernel/lib/stdio/vprintf.c @@ -0,0 +1,52 @@ +#include <stdarg.h> +#include <stdint.h> +#include <kernel/lib/stdio.h> +#include <kernel/lib/string.h> +#include <kernel/lib/stdlib.h> + +void __writes(const char *data) +{ + for (size_t i = 0; i < strlen(data); i++) writec(data[i]); +} + +void vprintf(const char *fmt, va_list args) +{ + uint8_t readyToFormat = 0; + + char buff = 0; + + for (; *fmt; fmt++) { + if (readyToFormat) { + if (*fmt == '%') { + writec('%'); + readyToFormat = 0; + continue; + } + + buff = *fmt; + if (buff == 's') { + const char *str = va_arg(args, const char*); + __writes(str); + readyToFormat = 0; + } else if (buff == 'x') { + char *p = htoa((uint32_t) va_arg(args, int)); + __writes(p); + kfree(p); + readyToFormat = 0; + } else if (buff == 'd') { + char *p = itoa(va_arg(args, int)); + __writes(p); + kfree(p); + readyToFormat = 0; + } else if (buff == 'c') { + writec((char) va_arg(args, int)); + readyToFormat = 0; + } + } else { + if (*fmt == '%') + readyToFormat = 1; + else + writec(*fmt); + } + } +}
\ No newline at end of file diff --git a/src/mlibc/stdio/writec.c b/src/kernel/lib/stdio/writec.c index 94d4fed..94d4fed 100644 --- a/src/mlibc/stdio/writec.c +++ b/src/kernel/lib/stdio/writec.c diff --git a/src/kernel/lib/stdlib.h b/src/kernel/lib/stdlib.h new file mode 100644 index 0000000..fb79f42 --- /dev/null +++ b/src/kernel/lib/stdlib.h @@ -0,0 +1,26 @@ +#ifndef MELVIX_STDLIB_H +#define MELVIX_STDLIB_H + +#include <stdint.h> + +#ifndef MELVIX_ALLOC_H + +#include <kernel/lib/stdlib/liballoc.h> + +#endif + +#ifndef MELVIX_STRING_H + +#include <kernel/lib/string.h> + +#endif + +char *itoa(int n); + +int atoi(char *str); + +char *htoa(uint32_t n); + +int htoi(char *str); + +#endif diff --git a/src/kernel/lib/stdlib/atoi.c b/src/kernel/lib/stdlib/atoi.c new file mode 100644 index 0000000..4044972 --- /dev/null +++ b/src/kernel/lib/stdlib/atoi.c @@ -0,0 +1,24 @@ +#include <kernel/lib/math.h> +#include <stddef.h> +#include <stdint.h> +#include <kernel/lib/string.h> + +int atoi(char *str) +{ + size_t s_str = strlen(str); + if (!s_str) return 0; + + uint8_t negative = 0; + if (str[0] == '-') negative = 1; + + size_t i = 0; + if (negative) i++; + + int ret = 0; + for (; i < s_str; i++) { + ret += (str[i] - '0') * pow(10, (s_str - i) - 1); + } + + if (negative) ret *= -1; + return ret; +}
\ No newline at end of file diff --git a/src/kernel/lib/stdlib/htoa.c b/src/kernel/lib/stdlib/htoa.c new file mode 100644 index 0000000..85bd750 --- /dev/null +++ b/src/kernel/lib/stdlib/htoa.c @@ -0,0 +1,30 @@ +#include <stdint.h> +#include <kernel/lib/string.h> +#include <kernel/lib/stdlib.h> + +static const char __HTOA_TABLE[] = "0123456789ABCDEF"; + +char *htoa(uint32_t n) +{ + char *ret = kmalloc(10); + + int i = 0; + while (n) { + ret[i++] = __HTOA_TABLE[n & 0xF]; + n >>= 4; + } + + if (!i) { + ret[0] = '0'; + i++; + } + + for (; i <= 9; i++) ret[i] = 0; + + char *aux = strdup(ret); + kfree(ret); + ret = aux; + + strinv(ret); + return ret; +}
\ No newline at end of file diff --git a/src/kernel/lib/stdlib/htoi.c b/src/kernel/lib/stdlib/htoi.c new file mode 100644 index 0000000..f2d4702 --- /dev/null +++ b/src/kernel/lib/stdlib/htoi.c @@ -0,0 +1,23 @@ +#include <kernel/lib/math.h> +#include <stddef.h> +#include <kernel/lib/string.h> + +int htoi(char *str) +{ + size_t s_str = strlen(str); + + size_t i = 0; + int ret = 0; + for (; i < s_str; i++) { + char c = str[i]; + int aux = 0; + if (c >= '0' && c <= '9') + aux = c - '0'; + else if (c >= 'A' && c <= 'F') + aux = (c - 'A') + 10; + + ret += aux * pow(16, (s_str - i) - 1); + } + + return ret; +}
\ No newline at end of file diff --git a/src/mlibc/stdlib/itoa.c b/src/kernel/lib/stdlib/itoa.c index 3c8ee05..897fd55 100644 --- a/src/mlibc/stdlib/itoa.c +++ b/src/kernel/lib/stdlib/itoa.c @@ -1,7 +1,7 @@ -#include <mlibc/math.h> +#include <kernel/lib/math.h> #include <stdint.h> -#include <mlibc/string.h> -#include <mlibc/stdlib.h> +#include <kernel/lib/string.h> +#include <kernel/lib/stdlib.h> #include <kernel/paging/paging.h> static const char __ITOA_TABLE[] = "0123456789"; diff --git a/src/mlibc/stdlib/liballoc.c b/src/kernel/lib/stdlib/liballoc.c index 3938215..41c55e1 100644 --- a/src/mlibc/stdlib/liballoc.c +++ b/src/kernel/lib/stdlib/liballoc.c @@ -1,7 +1,7 @@ #include <stddef.h> #include <stdint.h> #include <kernel/paging/paging.h> -#include <mlibc/stdlib/liballoc.h> +#include <kernel/lib/stdlib/liballoc.h> int liballoc_lock() { diff --git a/src/mlibc/stdlib/liballoc.h b/src/kernel/lib/stdlib/liballoc.h index 6ed9efb..6ed9efb 100644 --- a/src/mlibc/stdlib/liballoc.h +++ b/src/kernel/lib/stdlib/liballoc.h diff --git a/src/mlibc/string.h b/src/kernel/lib/string.h index fe925cb..fe925cb 100644 --- a/src/mlibc/string.h +++ b/src/kernel/lib/string.h diff --git a/src/kernel/lib/string/strcat.c b/src/kernel/lib/string/strcat.c new file mode 100644 index 0000000..e0d6885 --- /dev/null +++ b/src/kernel/lib/string/strcat.c @@ -0,0 +1,10 @@ +#include <kernel/lib/string.h> + +void strcat(char *dest, const char *orig) +{ + size_t s_dest = strlen(dest); + size_t s_orig = strlen(orig); + + for (size_t i = 0; i < s_orig; i++) dest[s_dest + i] = orig[i]; + dest[s_dest + s_orig] = 0; +}
\ No newline at end of file diff --git a/src/kernel/lib/string/strcati.c b/src/kernel/lib/string/strcati.c new file mode 100644 index 0000000..9ee3a43 --- /dev/null +++ b/src/kernel/lib/string/strcati.c @@ -0,0 +1,8 @@ +#include <kernel/lib/string.h> + +void strcati(char *dest, const char *orig) +{ + size_t s_orig = strlen(orig); + strdisp(dest, (int) s_orig); + for (size_t i = 0; i < s_orig; i++) dest[i] = orig[i]; +}
\ No newline at end of file diff --git a/src/kernel/lib/string/strcmp.c b/src/kernel/lib/string/strcmp.c new file mode 100644 index 0000000..b8ac2a7 --- /dev/null +++ b/src/kernel/lib/string/strcmp.c @@ -0,0 +1,10 @@ +#include <kernel/lib/string.h> + +char strcmp(const char *a, const char *b) +{ + if (strlen(a) != strlen(b)) return 1; + + for (size_t i = 0; i < strlen(a); i++) if (a[i] != b[i]) return 1; + + return 0; +}
\ No newline at end of file diff --git a/src/kernel/lib/string/strcpy.c b/src/kernel/lib/string/strcpy.c new file mode 100644 index 0000000..bab8535 --- /dev/null +++ b/src/kernel/lib/string/strcpy.c @@ -0,0 +1,9 @@ +#include <kernel/lib/string.h> + +void strcpy(char *dest, const char *orig) +{ + size_t s_orig = strlen(orig); + + for (size_t i = 0; i < s_orig; i++) dest[i] = orig[i]; + dest[s_orig] = 0; +}
\ No newline at end of file diff --git a/src/kernel/lib/string/strdisp.c b/src/kernel/lib/string/strdisp.c new file mode 100644 index 0000000..36c03c3 --- /dev/null +++ b/src/kernel/lib/string/strdisp.c @@ -0,0 +1,12 @@ +#include <kernel/lib/string.h> + +void strdisponce(char *str) +{ + for (size_t i = sizeof(str) + 2; i > 0; i--) str[i] = str[i - 1]; + str[0] = 0; +} + +void strdisp(char *str, int n) +{ + for (int i = 0; i < n; i++) strdisponce(str); +}
\ No newline at end of file diff --git a/src/mlibc/string/strdup.c b/src/kernel/lib/string/strdup.c index 0aa36f7..9330144 100644 --- a/src/mlibc/string/strdup.c +++ b/src/kernel/lib/string/strdup.c @@ -1,5 +1,5 @@ -#include <mlibc/string.h> -#include <mlibc/stdlib.h> +#include <kernel/lib/string.h> +#include <kernel/lib/stdlib.h> char *strdup(const char *orig) { diff --git a/src/kernel/lib/string/strinv.c b/src/kernel/lib/string/strinv.c new file mode 100644 index 0000000..b4b4212 --- /dev/null +++ b/src/kernel/lib/string/strinv.c @@ -0,0 +1,13 @@ +#include <kernel/lib/string.h> + +void strinv(char *str) +{ + size_t s_str = strlen(str); + + int iterations = (int) s_str / 2; + for (int i = 0; i < iterations; i++) { + char aux = str[i]; + str[i] = str[(s_str - i) - 1]; + str[(s_str - i) - 1] = aux; + } +}
\ No newline at end of file diff --git a/src/kernel/lib/string/strlen.c b/src/kernel/lib/string/strlen.c new file mode 100644 index 0000000..77e3c00 --- /dev/null +++ b/src/kernel/lib/string/strlen.c @@ -0,0 +1,8 @@ +#include <kernel/lib/string.h> + +size_t strlen(const char *str) +{ + size_t len = 0; + while (str[len]) len++; + return len; +}
\ No newline at end of file diff --git a/src/kernel/syscall/actions/sys_read.c b/src/kernel/syscall/actions/sys_read.c new file mode 100644 index 0000000..761eec7 --- /dev/null +++ b/src/kernel/syscall/actions/sys_read.c @@ -0,0 +1,21 @@ +#include <stdint-gcc.h> +#include <kernel/lib/stdio.h> +#include <kernel/lib/string.h> +#include <kernel/io/io.h> +#include <kernel/input/input.h> +#include <kernel/timer/timer.h> + +uint32_t sys_read() +{ + serial_write("CALL\n"); + + keyboard_clear_buffer(); + while (keyboard_buffer[strlen(keyboard_buffer) - 1] != '\n') { + { + timer_wait(1); // IDK why? + } + } + serial_write(keyboard_buffer); + serial_write("\n"); + return (uint32_t) keyboard_buffer; +}
\ No newline at end of file diff --git a/src/kernel/syscall/actions/sys_write.c b/src/kernel/syscall/actions/sys_write.c index 882ff19..aef2b2b 100644 --- a/src/kernel/syscall/actions/sys_write.c +++ b/src/kernel/syscall/actions/sys_write.c @@ -1,9 +1,11 @@ #include <stdint-gcc.h> -#include <mlibc/stdio.h> +#include <kernel/lib/stdio.h> +#include <kernel/lib/string.h> +#include <kernel/io/io.h> -uint32_t sys_write(unsigned int fd, unsigned int buf, unsigned int count) +uint32_t sys_write(unsigned int buf) { - for (uint32_t i = 0; i < count; i++) - writec(*((char *) buf++)); - return count; + serial_put(((char *) buf)[0]); + printf((const char *) buf); + return strlen((const char *) buf); }
\ No newline at end of file diff --git a/src/kernel/syscall/syscall.c b/src/kernel/syscall/syscall.c index 0eac619..eb44c87 100644 --- a/src/kernel/syscall/syscall.c +++ b/src/kernel/syscall/syscall.c @@ -6,11 +6,14 @@ typedef uint32_t (*syscall_func)(unsigned int, ...); uint32_t (*syscalls[])() = { - [1] = sys_write + [1] = sys_write, + [2] = sys_read }; void syscall_handler(struct regs *r) { + serial_write("Received syscall!\n"); + if (r->eax >= sizeof(syscalls) / sizeof(*syscalls)) return; @@ -18,7 +21,6 @@ void syscall_handler(struct regs *r) if (!location) return; - serial_write("Received syscall!\n"); location(r->ebx, r->ecx, r->edx, r->esi, r->edi); } diff --git a/src/kernel/syscall/syscall.h b/src/kernel/syscall/syscall.h index 8730dbf..8ed532d 100644 --- a/src/kernel/syscall/syscall.h +++ b/src/kernel/syscall/syscall.h @@ -5,6 +5,8 @@ extern void idt_syscall(); void syscalls_install(); -uint32_t sys_write(unsigned int buf, unsigned int count); +uint32_t sys_write(unsigned int buf); + +uint32_t sys_read(); #endif
\ No newline at end of file diff --git a/src/kernel/system.c b/src/kernel/system.c index 7d57f66..179b2a2 100644 --- a/src/kernel/system.c +++ b/src/kernel/system.c @@ -1,11 +1,11 @@ #include <kernel/timer/timer.h> #include <kernel/io/io.h> #include <kernel/graphics/vesa.h> -#include <mlibc/string.h> -#include <mlibc/stdlib.h> +#include <kernel/lib/string.h> +#include <kernel/lib/stdlib.h> #include <kernel/paging/paging.h> #include <kernel/interrupts/interrupts.h> -#include <mlibc/stdio.h> +#include <kernel/lib/stdio.h> #include <stdarg.h> char *vga_buffer = (char *) 0x500; diff --git a/src/userspace/main.c b/src/userspace/main.c index f7cd3ba..c85e64d 100644 --- a/src/userspace/main.c +++ b/src/userspace/main.c @@ -1,9 +1,15 @@ #include <syscall.h> +#include <mlibc/string.h> void user_main() { - const char hello[] = "> Successfully to usermode!\n"; - syscall_write(0, hello, sizeof(hello)); + char hello[] = "> Successfully switched to usermode!\n"; + syscall_write(hello); - while (1) {}; + while (1) { + char *command = (char *) syscall_read(); + char test[1024]; + strcpy(test, command); + syscall_write(command); + }; }
\ No newline at end of file diff --git a/src/userspace/mlibc/math.h b/src/userspace/mlibc/math.h new file mode 100644 index 0000000..c9abf7d --- /dev/null +++ b/src/userspace/mlibc/math.h @@ -0,0 +1,6 @@ +#ifndef MELVIX_MATH_H +#define MELVIX_MATH_H + +int pow(int base, int exp); + +#endif diff --git a/src/userspace/mlibc/math/pow.c b/src/userspace/mlibc/math/pow.c new file mode 100644 index 0000000..24670a0 --- /dev/null +++ b/src/userspace/mlibc/math/pow.c @@ -0,0 +1,10 @@ +int pow(int base, int exp) +{ + if (exp < 0) return 0; + + if (!exp) return 1; + + int ret = base; + for (int i = 1; i < exp; i++) ret *= base; + return ret; +}
\ No newline at end of file diff --git a/src/userspace/mlibc/stdio.h b/src/userspace/mlibc/stdio.h new file mode 100644 index 0000000..d102dd6 --- /dev/null +++ b/src/userspace/mlibc/stdio.h @@ -0,0 +1,16 @@ +#ifndef MELVIX_STDIO_H +#define MELVIX_STDIO_H + +#include <stdarg.h> + +char getch(); + +char *readline(); + +void writec(char c); + +void vprintf(const char *format, va_list args); + +void printf(const char *format, ...); + +#endif diff --git a/src/userspace/mlibc/stdio/getch.c b/src/userspace/mlibc/stdio/getch.c new file mode 100644 index 0000000..114b6e0 --- /dev/null +++ b/src/userspace/mlibc/stdio/getch.c @@ -0,0 +1,7 @@ +// #include <syscall.h> + +char getch() +{ + // return ((char *) syscall_read())[0]; + return 0; +}
\ No newline at end of file diff --git a/src/mlibc/stdio/printf.c b/src/userspace/mlibc/stdio/printf.c index 5617d03..5617d03 100644 --- a/src/mlibc/stdio/printf.c +++ b/src/userspace/mlibc/stdio/printf.c diff --git a/src/userspace/mlibc/stdio/readline.c b/src/userspace/mlibc/stdio/readline.c new file mode 100644 index 0000000..a7082e6 --- /dev/null +++ b/src/userspace/mlibc/stdio/readline.c @@ -0,0 +1,7 @@ +// #include <syscall.h> + +char *readline() +{ + // return (char *) syscall_read(); + return "0"; +}
\ No newline at end of file diff --git a/src/mlibc/stdio/vprintf.c b/src/userspace/mlibc/stdio/vprintf.c index 81efce2..92999b6 100644 --- a/src/mlibc/stdio/vprintf.c +++ b/src/userspace/mlibc/stdio/vprintf.c @@ -3,6 +3,7 @@ #include <mlibc/stdio.h> #include <mlibc/string.h> #include <mlibc/stdlib.h> +#include <mlibc/stdlib.h> void __writes(const char *data) { @@ -31,12 +32,12 @@ void vprintf(const char *fmt, va_list args) } else if (buff == 'x') { char *p = htoa((uint32_t) va_arg(args, int)); __writes(p); - kfree(p); + // kfree(p); readyToFormat = 0; } else if (buff == 'd') { char *p = itoa(va_arg(args, int)); __writes(p); - kfree(p); + // kfree(p); readyToFormat = 0; } else if (buff == 'c') { writec((char) va_arg(args, int)); diff --git a/src/userspace/mlibc/stdio/writec.c b/src/userspace/mlibc/stdio/writec.c new file mode 100644 index 0000000..9ffb1a1 --- /dev/null +++ b/src/userspace/mlibc/stdio/writec.c @@ -0,0 +1,4 @@ +void writec(char c) +{ + // +}
\ No newline at end of file diff --git a/src/mlibc/stdlib.h b/src/userspace/mlibc/stdlib.h index 36a3b5f..ff8603c 100644 --- a/src/mlibc/stdlib.h +++ b/src/userspace/mlibc/stdlib.h @@ -3,12 +3,6 @@ #include <stdint.h> -#ifndef MELVIX_ALLOC_H - -#include <mlibc/stdlib/liballoc.h> - -#endif - #ifndef MELVIX_STRING_H #include <mlibc/string.h> diff --git a/src/mlibc/stdlib/atoi.c b/src/userspace/mlibc/stdlib/atoi.c index 0643372..0643372 100644 --- a/src/mlibc/stdlib/atoi.c +++ b/src/userspace/mlibc/stdlib/atoi.c diff --git a/src/mlibc/stdlib/htoa.c b/src/userspace/mlibc/stdlib/htoa.c index ab20260..098e1c8 100644 --- a/src/mlibc/stdlib/htoa.c +++ b/src/userspace/mlibc/stdlib/htoa.c @@ -6,7 +6,8 @@ static const char __HTOA_TABLE[] = "0123456789ABCDEF"; char *htoa(uint32_t n) { - char *ret = kmalloc(10); + char *ret = 0; + //kmalloc(10); int i = 0; while (n) { @@ -22,7 +23,7 @@ char *htoa(uint32_t n) for (; i <= 9; i++) ret[i] = 0; char *aux = strdup(ret); - kfree(ret); + // kfree(ret); ret = aux; strinv(ret); diff --git a/src/mlibc/stdlib/htoi.c b/src/userspace/mlibc/stdlib/htoi.c index c78fe9d..489e5c0 100644 --- a/src/mlibc/stdlib/htoi.c +++ b/src/userspace/mlibc/stdlib/htoi.c @@ -1,5 +1,5 @@ -#include <mlibc/math.h> #include <stddef.h> +#include <mlibc/math.h> #include <mlibc/string.h> int htoi(char *str) diff --git a/src/userspace/mlibc/stdlib/itoa.c b/src/userspace/mlibc/stdlib/itoa.c new file mode 100644 index 0000000..55c1383 --- /dev/null +++ b/src/userspace/mlibc/stdlib/itoa.c @@ -0,0 +1,47 @@ +#include <stdint.h> +#include <mlibc/math.h> +#include <mlibc/string.h> +#include <mlibc/stdlib.h> + +static const char __ITOA_TABLE[] = "0123456789"; + +char *itoa(int n) +{ + //if (paging_enabled == 0) + // return "0"; // kmalloc isn't available + + if (!n) { + char *ret = 0; + //kmalloc(2); + ret[0] = '0'; + ret[1] = 0; + return ret; + } + uint8_t negative = (uint8_t) (n < 0); + if (negative) n *= -1; + + int sz; + for (sz = 0; n % pow(10, sz) != n; sz++) {} + + char *ret = 0; + //kmalloc(sz + 1); + + for (int i = 0; i < sz; i++) { + int digit = (n % pow(10, i + 1)) / pow(10, i); + ret[i] = __ITOA_TABLE[digit]; + } + ret[sz] = 0; + + if (negative) { + char *aux = 0; + //kmalloc(sz + 2); + strcpy(aux, ret); + aux[sz] = '-'; + aux[sz + 1] = 0; + // kfree(ret); + ret = aux; + } + + strinv(ret); + return ret; +}
\ No newline at end of file diff --git a/src/userspace/mlibc/string.h b/src/userspace/mlibc/string.h new file mode 100644 index 0000000..fe925cb --- /dev/null +++ b/src/userspace/mlibc/string.h @@ -0,0 +1,22 @@ +#ifndef MELVIX_STRING_H +#define MELVIX_STRING_H + +#include <stddef.h> + +size_t strlen(const char *str); + +void strcpy(char *dest, const char *orig); + +void strdisp(char *str, int n); + +void strcat(char *dest, const char *orig); + +void strcati(char *dest, const char *orig); + +char strcmp(const char *a, const char *b); + +char *strdup(const char *orig); + +void strinv(char *str); + +#endif diff --git a/src/mlibc/string/strcat.c b/src/userspace/mlibc/string/strcat.c index 0448430..0448430 100644 --- a/src/mlibc/string/strcat.c +++ b/src/userspace/mlibc/string/strcat.c diff --git a/src/mlibc/string/strcati.c b/src/userspace/mlibc/string/strcati.c index 8fdcc1a..8fdcc1a 100644 --- a/src/mlibc/string/strcati.c +++ b/src/userspace/mlibc/string/strcati.c diff --git a/src/mlibc/string/strcmp.c b/src/userspace/mlibc/string/strcmp.c index be6c17a..be6c17a 100644 --- a/src/mlibc/string/strcmp.c +++ b/src/userspace/mlibc/string/strcmp.c diff --git a/src/mlibc/string/strcpy.c b/src/userspace/mlibc/string/strcpy.c index 8dfa65f..8dfa65f 100644 --- a/src/mlibc/string/strcpy.c +++ b/src/userspace/mlibc/string/strcpy.c diff --git a/src/mlibc/string/strdisp.c b/src/userspace/mlibc/string/strdisp.c index d793718..d793718 100644 --- a/src/mlibc/string/strdisp.c +++ b/src/userspace/mlibc/string/strdisp.c diff --git a/src/userspace/mlibc/string/strdup.c b/src/userspace/mlibc/string/strdup.c new file mode 100644 index 0000000..bb3597d --- /dev/null +++ b/src/userspace/mlibc/string/strdup.c @@ -0,0 +1,11 @@ +#include <mlibc/string.h> +#include <mlibc/stdlib.h> + +char *strdup(const char *orig) +{ + // size_t s_orig = strlen(orig); + char *ret = 0; + // kmalloc(s_orig + 1); + strcpy(ret, orig); + return ret; +}
\ No newline at end of file diff --git a/src/mlibc/string/strinv.c b/src/userspace/mlibc/string/strinv.c index 71f3355..71f3355 100644 --- a/src/mlibc/string/strinv.c +++ b/src/userspace/mlibc/string/strinv.c diff --git a/src/mlibc/string/strlen.c b/src/userspace/mlibc/string/strlen.c index 133ee3d..133ee3d 100644 --- a/src/mlibc/string/strlen.c +++ b/src/userspace/mlibc/string/strlen.c diff --git a/src/userspace/syscall.h b/src/userspace/syscall.h index 162b219..6750093 100644 --- a/src/userspace/syscall.h +++ b/src/userspace/syscall.h @@ -62,11 +62,15 @@ /** * DECLARATIONS */ -DECL_SYSCALL3(write, int, const char *, int); +DECL_SYSCALL1(write, char *); + +DECL_SYSCALL0(read); /** * DEFINITIONS */ -DEFN_SYSCALL3(write, 1, int, const char *, int); +DEFN_SYSCALL1(write, 1, char *); + +DEFN_SYSCALL0(read, 2); #endif |