diff options
author | Benji Dial <benji6283@gmail.com> | 2020-09-06 00:48:07 -0400 |
---|---|---|
committer | Benji Dial <benji6283@gmail.com> | 2020-09-06 00:48:07 -0400 |
commit | e8c6577617bffa4402c07c7aa20e3c24f03c1c20 (patch) | |
tree | 2fb9230b62d2344a44453117de9e656892219788 /src/kernel | |
parent | 7ff724fe8f709440da9c730fdb8dcbaa4f989ed5 (diff) | |
download | portland-os-e8c6577617bffa4402c07c7aa20e3c24f03c1c20.tar.gz |
program loading, others
big kernel additions: paging, elf loading, separate kernel and user page allocation
it now properly loads and runs sd0:bin/init.elf
still need to determine which disk was booted from, and start the init on that disk
Diffstat (limited to 'src/kernel')
-rw-r--r-- | src/kernel/boot.h | 3 | ||||
-rw-r--r-- | src/kernel/drive.c | 23 | ||||
-rw-r--r-- | src/kernel/drive.h | 21 | ||||
-rw-r--r-- | src/kernel/elf-link.ld | 4 | ||||
-rw-r--r-- | src/kernel/elf.c | 116 | ||||
-rw-r--r-- | src/kernel/elf.h | 9 | ||||
-rw-r--r-- | src/kernel/fat.c | 53 | ||||
-rw-r--r-- | src/kernel/fat.h | 1 | ||||
-rw-r--r-- | src/kernel/ide.c | 32 | ||||
-rw-r--r-- | src/kernel/idt.c | 116 | ||||
-rw-r--r-- | src/kernel/idt.h | 9 | ||||
-rw-r--r-- | src/kernel/isrs.asm | 94 | ||||
-rw-r--r-- | src/kernel/main.c | 58 | ||||
-rw-r--r-- | src/kernel/main2.c | 18 | ||||
-rw-r--r-- | src/kernel/mem.c | 63 | ||||
-rw-r--r-- | src/kernel/mem.h | 13 | ||||
-rw-r--r-- | src/kernel/paging.c | 130 | ||||
-rw-r--r-- | src/kernel/paging.h | 17 | ||||
-rw-r--r-- | src/kernel/pci.c | 8 | ||||
-rw-r--r-- | src/kernel/plef.c | 32 | ||||
-rw-r--r-- | src/kernel/plef.h | 21 | ||||
-rw-r--r-- | src/kernel/pmap.c | 127 | ||||
-rw-r--r-- | src/kernel/pmap.h | 17 | ||||
-rw-r--r-- | src/kernel/serial.c | 5 | ||||
-rw-r--r-- | src/kernel/task.c | 90 | ||||
-rw-r--r-- | src/kernel/task.h | 26 | ||||
-rw-r--r-- | src/kernel/util.c | 2 | ||||
-rw-r--r-- | src/kernel/util.h | 2 | ||||
-rw-r--r-- | src/kernel/vga.c | 17 | ||||
-rw-r--r-- | src/kernel/vga.h | 1 |
30 files changed, 911 insertions, 217 deletions
diff --git a/src/kernel/boot.h b/src/kernel/boot.h index b3d3e76..9774573 100644 --- a/src/kernel/boot.h +++ b/src/kernel/boot.h @@ -4,7 +4,8 @@ #include <stdint.h> enum { - BIS_PCI = 0x80 + BIS_PCI = 0x80, + BIS_PAE = 0x40 }; enum { diff --git a/src/kernel/drive.c b/src/kernel/drive.c index 38fef38..c3aa5b2 100644 --- a/src/kernel/drive.c +++ b/src/kernel/drive.c @@ -2,23 +2,27 @@ #include "panic.h" #include "fat.h" -uint8_t n_drives = 0; +uint8_t n_drives; struct drive drives[256]; +void init_drives() { + n_drives = 0; +} + __attribute__ ((const)) -static drive_file_id_t unknown_get_file(const struct drive *d, const char *path) { +static file_id_t unknown_get_file(const struct drive *d, const char *path) { return 0; } -static void unknown_free_file(const struct drive *d, drive_file_id_t fid) { +static void unknown_free_file(const struct drive *d, file_id_t fid) { panic("Free file called on unknown file system"); } -static void unknown_load_sector(const struct drive *d, drive_file_id_t fid, uint32_t sector, void *at) { +static void unknown_load_sector(const struct drive *d, file_id_t fid, uint32_t sector, void *at) { panic("Load sector called on unknown file system"); } -static uint32_t unknown_get_file_length(const struct drive *d, drive_file_id_t fid) { +static uint32_t unknown_get_file_length(const struct drive *d, file_id_t fid) { panic("Get file length called on unknown file system"); } @@ -32,7 +36,7 @@ static uint32_t unknown_enumerate_dir(const struct drive *d, const char *path, s return 0; } -static void determine_fs(struct drive *d) { +static inline void determine_fs(struct drive *d) { if (try_fat_init_drive(d)) return; @@ -45,7 +49,12 @@ static void determine_fs(struct drive *d) { d->get_free_sectors = &unknown_get_free_sectors; } +//drive should be ready before this. +//determine_fs and its children +//do not need to make ready or done void commit_drive(struct drive data) { determine_fs(&data); - drives[n_drives++] = data; + drives[n_drives] = data; + data.done(drives + n_drives); + ++n_drives; }
\ No newline at end of file diff --git a/src/kernel/drive.h b/src/kernel/drive.h index 8eb6955..b02b4ad 100644 --- a/src/kernel/drive.h +++ b/src/kernel/drive.h @@ -4,7 +4,7 @@ #include <stdbool.h> #include <stdint.h> -typedef uint8_t drive_file_id_t; +typedef uint8_t file_id_t; typedef uint8_t fs_id_t; typedef uint8_t drive_id_t; @@ -22,23 +22,26 @@ struct drive { char *drive_type; char *fs_type; - uint8_t (*read_sectors) (const struct drive *d, uint32_t start, uint32_t count, void *buffer); - uint8_t (*write_sectors)(const struct drive *d, uint32_t start, uint32_t count, const void *buffer); + uint32_t (*read_sectors) (const struct drive *d, uint32_t start, uint32_t count, void *buffer); + uint32_t (*write_sectors)(const struct drive *d, uint32_t start, uint32_t count, const void *buffer); + void (*ready) (const struct drive *d); + void (*done) (const struct drive *d); uint32_t n_sectors; drive_id_t drive_id; - drive_file_id_t (*get_file) (const struct drive *d, const char *path); - void (*free_file) (const struct drive *d, drive_file_id_t fid); - void (*load_sector) (const struct drive *d, drive_file_id_t fid, uint32_t sector, void *at); - uint32_t (*get_file_length) (const struct drive *d, drive_file_id_t fid); - uint32_t (*enumerate_dir) (const struct drive *d, const char *path, struct directory_content_info *info, uint32_t max); - uint32_t (*get_free_sectors)(const struct drive *d); + file_id_t (*get_file) (const struct drive *d, const char *path); + void (*free_file) (const struct drive *d, file_id_t fid); + void (*load_sector) (const struct drive *d, file_id_t fid, uint32_t sector, void *at); + uint32_t (*get_file_length) (const struct drive *d, file_id_t fid); + uint32_t (*enumerate_dir) (const struct drive *d, const char *path, struct directory_content_info *info, uint32_t max); + uint32_t (*get_free_sectors)(const struct drive *d); fs_id_t fs_id; }; extern uint8_t n_drives; extern struct drive drives[MAX_DRIVES]; +void init_drives(); void commit_drive(struct drive data); #endif
\ No newline at end of file diff --git a/src/kernel/elf-link.ld b/src/kernel/elf-link.ld index a0d60f9..10abac0 100644 --- a/src/kernel/elf-link.ld +++ b/src/kernel/elf-link.ld @@ -13,9 +13,9 @@ SECTIONS { .data : { *(.data) } - . = 0x10000000; + . = 0x04000000; .bss : { *(.bss) - kernel_bss_end = .; + _kernel_bss_end = .; } }
\ No newline at end of file diff --git a/src/kernel/elf.c b/src/kernel/elf.c new file mode 100644 index 0000000..8cd659c --- /dev/null +++ b/src/kernel/elf.c @@ -0,0 +1,116 @@ +#include <stdint.h> +#include "drive.h" +#include "elf.h" +#include "task.h" +#include "util.h" +#include "paging.h" +#include "pmap.h" + +#define ELF_MAGIC 0x464c457f + +enum { + ELF_32 = 1, + ELF_64 = 2 +}; + +enum { + LITTLE_ENDIAN = 1, + BIG_ENDIAN = 2 +}; + +enum { + //... + ARCH_X86 = 0x03 + //... +}; + +struct elf_header { + uint32_t magic; + uint8_t word_size; + uint8_t endianness; + uint8_t elf_version; + uint8_t target_os;//ignored + uint8_t os_abi_version;//should be zero + uint8_t reserved[7]; + uint16_t object_type;//TODO + uint16_t architecture;//should be ARCH_X86 + uint32_t elf_version_2; + uint32_t entry_vma; + uint32_t phtable_fa; + uint32_t shtable_fa; + uint32_t flags; + uint16_t eh_size; + uint16_t phentry_size; + uint16_t phtable_count; + uint16_t shentry_size; + uint16_t shtable_count; + uint16_t sh_names_entry; +} __attribute__ ((packed)); + +enum { + PT_UNUSED = 0, + PT_LOADME = 1, + PT_DYNLINK = 2, + PT_INTERP = 3, + PT_COMMENT = 4, + PT_SHARED = 5, + PT_PHTABLE = 6, + PT_TL_TMPL = 7 +}; + +enum { + PH_WRITABLE = 0x02 +}; + +struct ph_entry { + uint32_t type; + uint32_t fa; + uint32_t vma;//must be page-aligned + uint32_t pma;//ignored + uint32_t fs; + uint32_t vms; + uint32_t flags; + uint32_t align; +} __attribute__ ((packed)); + +bool try_elf_run(const struct drive *d, const char *path) { + file_id_t h = d->get_file(d, path); + if (!h) + return false; + + struct elf_header ehead; + + fmcpy(&ehead, d, h, 0, sizeof(struct elf_header)); + + if ((ehead.magic != ELF_MAGIC) || + (ehead.word_size != ELF_32) || + (ehead.endianness != LITTLE_ENDIAN) || + ehead.os_abi_version) { + d->free_file(d, h); + return false; + } + + uint32_t phtable_size = ehead.phentry_size * ehead.phtable_count; + uint16_t phtable_pages = (phtable_size - 1) / 4096 + 1; + + void *phtable = allocate_kernel_pages(phtable_pages); + fmcpy(phtable, d, h, ehead.phtable_fa, phtable_size); + + void *pd = new_task_pd(); + + for (uint32_t phi = 0; phi < ehead.phtable_count; ++phi) { + struct ph_entry *entry = phtable + phi * ehead.phentry_size; + if (entry->type != PT_LOADME) + continue; + void *pma = pd_user_allocate(pd, entry->vma, (entry->vms - 1) / 4096 + 1, entry->flags & PH_WRITABLE); + fmcpy(pma, d, h, entry->fa, entry->fs); + } + + free_pages(phtable, phtable_pages); + + struct task_state tstate; + tstate.page_directory = pd; + tstate.ret_addr = ehead.entry_vma; + new_task(tstate); + return true; +}
\ No newline at end of file diff --git a/src/kernel/elf.h b/src/kernel/elf.h new file mode 100644 index 0000000..09aa05c --- /dev/null +++ b/src/kernel/elf.h @@ -0,0 +1,9 @@ +#ifndef ELF_H +#define ELF_H + +#include <stdint.h> +#include "drive.h" + +bool try_elf_run(const struct drive *d, const char *path); + +#endif
\ No newline at end of file diff --git a/src/kernel/fat.c b/src/kernel/fat.c index ff01d7d..1b6c52d 100644 --- a/src/kernel/fat.c +++ b/src/kernel/fat.c @@ -3,7 +3,7 @@ #include "util.h" #include "ata.h" #include "fat.h" -#include "mem.h" +#include "pmap.h" #define MAX_FAT_DRIVES 16 #define MAX_OPEN_FILES_PER_DRIVE 32 @@ -81,14 +81,14 @@ struct fat_drive_info { }; static struct fat_drive_info infos[MAX_FAT_DRIVES]; -static uint8_t next_id = 0; +static uint8_t next_id; static uint8_t fat_driver_buffer[512]; static struct fat_info *next_fi; static void alloc_next_fi() { if (!((uint32_t)(next_fi = (struct fat_info *)((uint32_t)next_fi + 64)) & 0xfff)) - if (!(next_fi = allocate_pages(1))) + if (!(next_fi = allocate_kernel_pages(1))) panic("Out of memory in FAT driver."); } @@ -109,8 +109,10 @@ static void load_cluster(uint16_t c, void *to) { cur_drive->read_sectors(cur_drive, s, 1, to); } +__attribute__ ((pure)) static uint16_t next_cluster(uint16_t c) { - panic("TODO: compute next sector (or 0 for none)"); + uint16_t found = infos[cur_id].fat[c]; + return (found < 2) || (found >= 0xfff0) ? 0 : found; } static const uint8_t this_dir[] = {'.', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}; @@ -182,7 +184,7 @@ static const char *split_path(const char *path, uint8_t *fat_name_buffer) { } else panic("Bad path in FAT16 driver"); - else if ((fi == 8) || (fi == 11)) + else if (((fi == 8) && (path[pi - 1] != EXT_SEP_CHAR)) || (fi == 11)) panic("Bad path in FAT16 driver"); else { fat_name_buffer[fi++] = (uint8_t)path[pi++]; @@ -209,13 +211,17 @@ static bool try_load_from_path(const struct drive *d, const char *path) { return true; } -static drive_file_id_t fat_get_file(const struct drive *d, const char *path) { +static file_id_t fat_get_file(const struct drive *d, const char *path) { + d->ready(d); struct open_file_info *open_files = infos[d->drive_id].open_files - 1; - for (drive_file_id_t n = 1; n != MAX_OPEN_FILES_PER_DRIVE + 1; ++n) + for (file_id_t n = 1; n != MAX_OPEN_FILES_PER_DRIVE + 1; ++n) if (!open_files[n].di_sector) { - if (!try_load_from_path(d, path)) + if (!try_load_from_path(d, path)) { + d->done(d); return 0; + } + d->done(d); open_files[n].di_sector = cur_sect; open_files[n].di_number = cur_dir - (struct directory_entry *)fat_driver_buffer; open_files[n].start_cluster = cur_dir->first_cluster; @@ -226,22 +232,26 @@ static drive_file_id_t fat_get_file(const struct drive *d, const char *path) { panic("Maximum number of files open reached for FAT drive."); } -static void fat_free_file(const struct drive *d, drive_file_id_t fid) { +static void fat_free_file(const struct drive *d, file_id_t fid) { infos[d->drive_id].open_files[fid - 1].di_sector = 0; } -static void fat_load_sector(const struct drive *d, drive_file_id_t fid, uint32_t sector, void *at) { +static void fat_load_sector(const struct drive *d, file_id_t fid, uint32_t sector, void *at) { cur_drive = d; cur_id = d->drive_id; cur_fdi = &infos[cur_id]; + uint16_t c = cur_fdi->open_files[fid - 1].start_cluster; for (uint32_t i = 0; i < sector; ++i) c = next_cluster(c); + + d->ready(d); load_cluster(c, at); + d->done(d); } __attribute__ ((pure)) -static uint32_t fat_get_file_length(const struct drive *d, drive_file_id_t fid) { +static uint32_t fat_get_file_length(const struct drive *d, file_id_t fid) { return infos[d->drive_id].open_files[fid - 1].length; } @@ -288,8 +298,10 @@ static uint32_t enumerate_root(const struct drive *d, struct directory_content_i d->read_sectors(d, sect, 1, entry); } - if (!*(uint8_t *)entry || (info == fill + max)) + if (!*(uint8_t *)entry || (info == fill + max)) { + d->done(d); return fill - info; + } if (entry-> attrib & FA_LABEL) { ++entry; @@ -306,11 +318,15 @@ static uint32_t enumerate_root(const struct drive *d, struct directory_content_i } static uint32_t fat_enumerate_dir(const struct drive *d, const char *path, struct directory_content_info *info, uint32_t max) { + d->ready(d); + if (!*path) return enumerate_root(d, info, max); - if (!try_load_from_path(d, path)) + if (!try_load_from_path(d, path)) { + d->done(d); return 0; + } uint16_t cluster = cur_dir->first_cluster; load_cluster(cluster, fat_driver_buffer); @@ -323,8 +339,10 @@ static uint32_t fat_enumerate_dir(const struct drive *d, const char *path, struc load_cluster(cluster = next_cluster(cluster), fat_driver_buffer); } - if (!*(uint8_t *)entry || (fill == info + max)) + if (!*(uint8_t *)entry || (fill == info + max)) { + d->done(d); return fill - info; + } if (check_fat_names(entry->name, this_dir) || check_fat_names(entry->name, parent_dir)) { ++entry; @@ -341,7 +359,8 @@ static uint32_t fat_enumerate_dir(const struct drive *d, const char *path, struc } void init_fat() { - next_fi = allocate_pages(1); + next_fi = allocate_kernel_pages(1); + next_id = 0; } bool try_fat_init_drive(struct drive *d) { @@ -366,7 +385,7 @@ bool try_fat_init_drive(struct drive *d) { d->fs_id = next_id; infos[next_id].fi = next_fi; - infos[next_id].fat = allocate_pages(((next_fi->sectors_per_fat - 1) >> 3) + 1); + infos[next_id].fat = allocate_kernel_pages(((next_fi->sectors_per_fat - 1) >> 3) + 1); infos[next_id].root_start = next_fi->reserved_sectors + next_fi->sectors_per_fat * next_fi->fats; infos[next_id].data_start = infos[next_id].root_start + @@ -374,7 +393,7 @@ bool try_fat_init_drive(struct drive *d) { d->read_sectors(d, next_fi->reserved_sectors, next_fi->sectors_per_fat, infos[next_id].fat); struct open_file_info *open_files = infos[next_id].open_files - 1; - for (drive_file_id_t i = 0; i < MAX_OPEN_FILES_PER_DRIVE; ++i) + for (file_id_t i = 0; i < MAX_OPEN_FILES_PER_DRIVE; ++i) open_files[i].di_sector = 0; alloc_next_fi(); diff --git a/src/kernel/fat.h b/src/kernel/fat.h index cadb073..3ca9cc3 100644 --- a/src/kernel/fat.h +++ b/src/kernel/fat.h @@ -2,7 +2,6 @@ #define FAT_H #include <stdbool.h> -#include <stdint.h> #include "drive.h" void init_fat(); diff --git a/src/kernel/ide.c b/src/kernel/ide.c index a4ba34c..70b1dee 100644 --- a/src/kernel/ide.c +++ b/src/kernel/ide.c @@ -15,7 +15,7 @@ struct ide_drive_info { }; static struct ide_drive_info ide_drives[MAX_IDE_DRIVES]; -static drive_id_t n_ide_drives = 0; +static drive_id_t n_ide_drives; typedef uint16_t spinner_t; @@ -53,7 +53,7 @@ static uint8_t wait_for_data_ready_not_busy(uint16_t base_port) { panic("Spun out in IDE driver."); } -static uint8_t ide_ata_rs(const struct drive *d, uint32_t start, uint32_t count, void *buffer) { +static uint32_t ide_ata_rs(const struct drive *d, uint32_t start, uint32_t count, void *buffer) { if (start >= d->n_sectors) return 0; if (start + count > d->n_sectors) @@ -91,21 +91,29 @@ static uint8_t ide_ata_rs(const struct drive *d, uint32_t start, uint32_t count, return count; } -static uint8_t ide_ata_ws(const struct drive *d, uint32_t start, uint32_t count, const void *buffer) { +static uint32_t ide_ata_ws(const struct drive *d, uint32_t start, uint32_t count, const void *buffer) { panic("IDE ATA writing not implemented yet"); return 0; } -static uint8_t ide_atapi_rs(const struct drive *d, uint32_t start, uint32_t count, void *buffer) { - //panic("IDE ATAPI reading not implemented yet"); - return 0; +static uint32_t ide_atapi_rs(const struct drive *d, uint32_t start, uint32_t count, void *buffer) { + if (start >= d->n_sectors) + return 0; + if (start + count > d->n_sectors) + count = d->n_sectors - start; + if (!count) + return 0; + + } -static uint8_t ide_atapi_ws(const struct drive *d, uint32_t start, uint32_t count, const void *buffer) { +static uint32_t ide_atapi_ws(const struct drive *d, uint32_t start, uint32_t count, const void *buffer) { panic("IDE ATAPI writing not implemented yet"); return 0; } +static void nop(const struct drive *d) { } + struct id_space { uint8_t skip1[120]; uint32_t max_lba;//120 @@ -132,9 +140,13 @@ static void test_drive(uint16_t base_port, uint16_t alt_port, bool slave) { outb(base_port | ATA_REG_CMD, ATA_CMD_ID); - if (!inb(base_port | ATA_REG_STATUS)) + uint8_t status = inb(base_port | ATA_REG_STATUS); + if (!status || (status == 0x7f)) return; + next_d.ready = &nop; + next_d.done = &nop; + struct id_space ids; if (!(wait_for_error_or_ready(base_port) & ATA_STATUS_ERROR)) { @@ -157,7 +169,7 @@ static void test_drive(uint16_t base_port, uint16_t alt_port, bool slave) { next_d.write_sectors = &ide_atapi_ws; //TODO: issue proper identify command - ids.max_lba = -1; + return; } else if (code == 0xc33c) { next_d.drive_type = "IDE SATA"; @@ -177,6 +189,8 @@ static void test_drive(uint16_t base_port, uint16_t alt_port, bool slave) { } void init_ide() { + n_ide_drives = 0; + uint16_t check_from = 0; struct pci_device *device; //double parentheses to let gcc know this assignment isn't a mistake diff --git a/src/kernel/idt.c b/src/kernel/idt.c new file mode 100644 index 0000000..460cfb0 --- /dev/null +++ b/src/kernel/idt.c @@ -0,0 +1,116 @@ +#include "drive.h" +#include "elf.h" +#include "util.h" +#include "idt.h" +#include "log.h" +#include "panic.h" +#include "task.h" +#include "paging.h" + +enum { + IDT_PRESENT = 0x80, + + IDT_INT = 0x0e, + IDT_TRAP = 0x0f +}; + +struct idt_entry { + uint16_t addr_low; + uint16_t cs; + uint8_t zero; + uint8_t flags; + uint16_t addr_high; +} __attribute__ ((packed)); + +struct idt_entry idt[256]; + +struct { + uint16_t limit; + uint32_t start; +} __attribute__ ((packed)) idtr = { + .limit = 256 * sizeof(struct idt_entry) - 1, + .start = (uint32_t)idt +}; + +//file handles as (drive_number << 8) + file_id_t + +static uint32_t sc_open_file(uint32_t drive_number, char *path) { + return (drive_number << 8) + drives[drive_number].get_file(drives + drive_number, path); +} + +static void sc_close_file(uint32_t handle) { + drives[handle >> 8].free_file(drives + (handle >> 8), handle & 0xff); +} + +static uint32_t sc_file_get_size(uint32_t handle) { + return drives[handle >> 8].get_file_length(drives + (handle >> 8), handle & 0xff); +} + +static uint32_t sc_file_read(uint32_t handle, uint32_t file_offset, uint32_t count, void *buffer) { + uint32_t len = sc_file_get_size(handle); + if (file_offset + count > len) + count = len - file_offset; + fmcpy(buffer, drives + (handle >> 8), handle & 0xff, file_offset, count); + return count; +} + +static bool sc_start_task(uint32_t drive_number, char *path) { + switch_to_kernel_cr3(); + bool result = try_elf_run(drives + drive_number, path); + switch_to_task_cr3(); + return result; +} + +static void sc_log_string(char *sz) { + logsz(sz); +} + +static char sc_get_key() { + panic("TODO: get key system call"); +} + +static void *sc_allocate_ram(uint32_t pages) { +//switch_to_kernel_cr3(); + void *result = pd_user_allocate_anywhere_writable(active_task->page_directory, pages); +//switch_to_task_cr3(); +} + +void const *syscall_table[] = { + &sc_open_file, + &sc_close_file, + &sc_file_read, + &sc_file_get_size, + &sc_start_task, + &sc_log_string, + &sc_get_key, + &sc_allocate_ram +}; + +extern void syscall_isr; +extern void quit_isr; +extern void yield_isr; + +void register_int(uint8_t n, void *isr, uint8_t dpl) { + idt[n].addr_low = (uint32_t)isr & 0xffff; + idt[n].addr_high = (uint32_t)isr >> 16; + idt[n].cs = 0x10; + idt[n].flags = IDT_PRESENT | (dpl << 5) | IDT_INT; +} + +void init_idt() { + for (uint16_t i = 0; i < 256; ++i) { + idt[i].flags = 0; + idt[i].zero = 0; + } + + register_int(0x30, &syscall_isr, 3); + register_int(0x38, &quit_isr, 3); + register_int(0x39, &yield_isr, 3); + + outb(0x0021, 0xff); + outb(0x00a1, 0xff); + + asm volatile ( + "lidt %0" + : : "m" (idtr) : "al"); +}
\ No newline at end of file diff --git a/src/kernel/idt.h b/src/kernel/idt.h new file mode 100644 index 0000000..6815de9 --- /dev/null +++ b/src/kernel/idt.h @@ -0,0 +1,9 @@ +#ifndef IDT_H +#define IDT_H + +#include <stdint.h> + +void init_idt(); +void enable_idt(); + +#endif
\ No newline at end of file diff --git a/src/kernel/isrs.asm b/src/kernel/isrs.asm new file mode 100644 index 0000000..163ddbe --- /dev/null +++ b/src/kernel/isrs.asm @@ -0,0 +1,94 @@ +bits 32 + +global syscall_isr +global quit_isr +global yield_isr +global _start_user_mode + +extern syscall_table +extern active_task + +extern delete_task +extern advance_active_task + +n_syscalls equ 8 + +section .text +syscall_isr: + cmp eax, n_syscalls + jge .bad + + mov eax, dword [syscall_table + eax * 4] + + push edi + push esi + push edx + push ecx + push ebx + + call eax + + add esp, 20 + + iret + +.bad: + mov eax, -1 + iret + +quit_isr: + push dword [active_task] + call delete_task + push yield_isr.return_to_task + jmp advance_active_task + +yield_isr: + mov eax, dword [active_task] + + mov dword [eax + 8], ebx + mov dword [eax + 12], ecx + mov dword [eax + 16], edx + mov dword [eax + 20], esi + mov dword [eax + 24], edi + mov dword [eax + 28], ebp + + mov edx, dword [esp] + mov dword [eax], edx + + mov edx, cr3 + mov dword [eax + 4], edx + + mov edx, dword [esp + 12] + mov dword [eax + 4], edx + + call advance_active_task + +.return_to_task: + mov eax, dword [active_task] + + mov edx, dword [eax] + mov dword [esp], edx + + mov edx, dword [eax + 4] + mov cr3, edx + + mov edx, dword [eax + 4] + mov dword [esp + 24], edx + + mov ebx, dword [eax + 8] + mov ecx, dword [eax + 12] + mov edx, dword [eax + 16] + mov esi, dword [eax + 20] + mov edi, dword [eax + 24] + mov ebp, dword [eax + 28] + +_before_start_task: + iret + +_start_user_mode: + push dword 0x2b + sub esp, 4 + push dword 0x00000200;interrupt flag + push dword 0x23 + sub esp, 4 + jmp yield_isr.return_to_task
\ No newline at end of file diff --git a/src/kernel/main.c b/src/kernel/main.c index 0c57ef3..eb57a01 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -1,21 +1,30 @@ #include <stdint.h> #include "serial.h" #include "panic.h" +#include "paging.h" #include "boot.h" #include "util.h" #include "fat.h" #include "ide.h" -#include "mem.h" +#include "idt.h" +#include "pmap.h" +#include "task.h" #include "pci.h" #include "log.h" +#include "elf.h" +void reset_tree(); void tree(struct drive *d); +void _start_user_mode() __attribute__ ((noreturn)); + __attribute__ ((noreturn)) void main() { char nbuf[11]; - init_mmap(); + init_pagemap(); + init_paging(); + init_tasks(); init_serial(); init_log(); @@ -57,6 +66,8 @@ void main() { init_fat(); //other fs drivers + init_drives(); + init_ide(); //other drive drivers @@ -97,13 +108,44 @@ void main() { logch('\n'); - u32_dec(pages_left * 4, nbuf); + for (uint8_t n = 0; n < n_drives; ++n) { + if (drives[n].get_free_sectors(drives + n) == -1) + continue; + + u32_dec(n, nbuf); + logsz("sd"); + logsz(nbuf); + logsz(" tree:\n"); + + reset_tree(); + tree(drives + n); + logch('\n'); + } + + if (!try_elf_run(drives, "BIN/INIT.ELF")) + panic("Failed to load init program."); + + if (BOOT_INFO->support_flags & BIS_PAE) + logsz("Processor supports PAE (but Portland OS does not yet).\n\n"); + else + logsz("Processor does not support PAE.\n\n"); + + u32_dec(kernel_pages_left * 4, nbuf); + logsz(nbuf); + logsz("k / "); + u32_dec(max_kernel_pages * 4, nbuf); + logsz(nbuf); + logsz("k kernel heap free.\n"); + u32_dec(user_pages_left * 4, nbuf); + logsz(nbuf); + logsz("k / "); + u32_dec(max_user_pages * 4, nbuf); logsz(nbuf); - logsz("k dynamic memory free.\n\n"); + logsz("k user memory free.\n"); - logsz("sd0 root:\n"); - tree(&drives[0]); +//while (1) +// asm ("hlt"); - while (1) - asm ("hlt"); + init_idt(); + _start_user_mode(); }
\ No newline at end of file diff --git a/src/kernel/main2.c b/src/kernel/main2.c index 2b85592..27ef8de 100644 --- a/src/kernel/main2.c +++ b/src/kernel/main2.c @@ -5,11 +5,21 @@ static char nbuf2[11]; -static char path_builder[200] = ""; -static uint8_t path_builder_len = 0; +static char path_builder[200]; +static uint8_t path_builder_len; -static char indent_builder[20] = " "; -static uint8_t indent_builder_len = 2; +static char indent_builder[20]; +static uint8_t indent_builder_len; + +void reset_tree() { + path_builder[0] = '\0'; + path_builder_len = 0; + + indent_builder[0] = ' '; + indent_builder[1] = ' '; + indent_builder[2] = '\0'; + indent_builder_len = 2; +} void tree(struct drive *d) { struct directory_content_info infos[100]; diff --git a/src/kernel/mem.c b/src/kernel/mem.c deleted file mode 100644 index d3baad3..0000000 --- a/src/kernel/mem.c +++ /dev/null @@ -1,63 +0,0 @@ -#include <stdint.h> -#include "panic.h" -#include "mem.h" - -#define DYNAMIC_START (0x10000000) -#define DYNAMIC_END (DYNAMIC_START + 65536 * 4096) -#define PAGE_TO_ADDR(n) ((void *)(DYNAMIC_START | ((n) << 12))) -#define ADDR_TO_PAGE(n) (((uint32_t)(n) & ~DYNAMIC_START) >> 12) - -#define MMAP_START (0x00010000) -#define PAGE_USED(n) ((*(uint8_t *)(MMAP_START + (n >> 3)) >> (n & 7)) & 1) -#define CLEAR_PAGE(n) *(uint8_t *)(MMAP_START + (n >> 3)) &= ~(1 << (n & 7)) -#define SET_PAGE(n) *(uint8_t *)(MMAP_START + (n >> 3)) |= 1 << (n & 7) - -extern const void kernel_bss_end; - -uint16_t pages_left; - -void init_mmap() { - volatile uint8_t *end_ptr = (uint8_t *)(DYNAMIC_END - 1); - uint8_t end_val = *end_ptr; - *end_ptr = (uint8_t)~end_val; - if (*end_ptr != (uint8_t)~end_val) - panic("Not enough memory. Must have at least 512MiB."); - - for (uint32_t *m = (uint32_t *)MMAP_START; m < (uint32_t *)(MMAP_START + (65536 / 8)); ++m) - *m = 0; - - uint16_t kernel_bss_pages = (((uint32_t)&kernel_bss_end - DYNAMIC_START - 1) >> 12) + 1; - for (uint16_t i = 0; i < kernel_bss_pages; ++i) - SET_PAGE(i); - - pages_left = 65536 - kernel_bss_pages; -} - -//very inneficient algorithm, just returns first hole big enough. -//a smarter algorithm might pick the smallest one available, -//and go by bytes (or dwords) instead of bits where possible. -void *allocate_pages(uint16_t n) { - uint16_t run = 0; - - for (uint32_t page = 0; page < 65536; ++page) { - if (PAGE_USED(page)) - run = 0; - else if (++run == n) { - uint16_t start = page - run + 1; - for (uint32_t i = start; i <= page; ++i) - SET_PAGE(i); - pages_left -= n; - return PAGE_TO_ADDR(start); - } - } - - return 0; -} - -//in the future, change this to go by bytes or dwords instead of bits. -void free_pages(const void *ptr, uint16_t n) { - uint16_t page = ADDR_TO_PAGE(ptr); - for (uint32_t i = page; i < page + n; ++i) - CLEAR_PAGE(i); - pages_left += n; -}
\ No newline at end of file diff --git a/src/kernel/mem.h b/src/kernel/mem.h deleted file mode 100644 index 9a9ae79..0000000 --- a/src/kernel/mem.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef MEM_H -#define MEM_H - -#include <stdint.h> - -extern uint16_t pages_left; - -void init_mmap(); - -void *allocate_pages(uint16_t n) __attribute__ ((malloc)); -void free_pages(const void *ptr, uint16_t n); - -#endif
\ No newline at end of file diff --git a/src/kernel/paging.c b/src/kernel/paging.c new file mode 100644 index 0000000..b92e037 --- /dev/null +++ b/src/kernel/paging.c @@ -0,0 +1,130 @@ +#include <stdbool.h> +#include <stdint.h> +#include "pmap.h" +#include "paging.h" +#include "boot.h" +#include "panic.h" +#include "task.h" + +enum { + PE_ADDR_MASK = 0xfffff000, + + PT_GLOBAL = 0x100, + PD_LARGE = 0x080, + PT_DIRTY = 0x040, + PE_ACCESSED = 0x020, + PE_NO_CACHE = 0x010, + PE_WRTHCH = 0x008, + PE_USER = 0x004, + PE_WRITABLE = 0x002, + PE_PRESENT = 0x001 +}; + +//TODO: +// try_elf_run needs to call these new functions. + +void *new_pd() { + uint32_t *pd = allocate_kernel_pages(1); + for (uint16_t i = 0; i < 1024; ++i) + pd[i] = 0; + return pd; +} + +void free_pd(void *pd) { + uint32_t *pd_32 = pd; + for (uint16_t i = 0; i < 1024; ++i) + if (pd_32[i] & PE_PRESENT) + free_pages((void *)(pd_32[i] & PE_ADDR_MASK), 1); + free_pages(pd, 1); +} + +void pd_map(void *pd, uint32_t physical_addr, uint32_t virtual_addr, bool writable) { + uint32_t *ptp = (uint32_t *)pd + (virtual_addr >> 22); + if (!(*ptp & PE_PRESENT)) + *ptp = (uint32_t)allocate_kernel_pages(1) | PE_USER | PE_WRITABLE | PE_PRESENT; + ((uint32_t *)(*ptp & PE_ADDR_MASK))[(virtual_addr >> 12) % 1024] = physical_addr | PE_USER | PE_PRESENT | (PE_WRITABLE * writable); +} + +bool pd_is_mapped(void *pd, uint32_t vma) { + uint32_t pde = ((uint32_t *)pd)[vma >> 22]; + return (pde & PE_PRESENT) && (((uint32_t *)(pde & PE_ADDR_MASK))[(vma >> 12) % 1024] & PE_PRESENT); +} + +#define KERNEL_END (0x08000000) + +void free_task_pd(void *pd) { + uint32_t *pd_32 = pd; + for (uint16_t i = 0; i < 1024; ++i) + if (pd_32[i] & PE_PRESENT) { + uint32_t *pt_32 = (uint32_t *)(pd_32[i] & PE_ADDR_MASK); + if (i >= KERNEL_END >> 22) + for (uint16_t j = 0; j < 1024; ++j) + if (pt_32[j] & PE_PRESENT) + free_pages((void *)(pt_32[j] & PE_ADDR_MASK), 1); + free_pages(pt_32, 1); + } + free_pages(pd, 1); +} + +void *new_task_pd() { + uint32_t *pd = new_pd(); + for (uint32_t addr = 0; addr < KERNEL_END; addr += 4096) + pd_map(pd, addr, addr, false); + return pd; +} + +void *pd_user_allocate(void *pd, uint32_t vma, uint32_t pages, bool writable) { + void *pma = allocate_user_pages(pages); + if (!pma) + panic("Could not allocate user pages."); + for (uint32_t i = 0; i < pages; ++i) + pd_map(pd, (uint32_t)pma + (i << 12), vma + (i << 12), writable); + return pma; +} + +void *pd_user_allocate_anywhere_writable(void *pd, uint32_t pages) { + uint32_t run = 0; + for (void *vma = (void *)KERNEL_END; vma; vma += 4096) { + if (pd_is_mapped(pd, vma)) + run = 0; + else if (++run == pages) { + vma -= (pages - 1) * 4096; + for (uint32_t i = 0; i < pages; ++i) + pd_map(pd, (uint32_t)allocate_user_pages(1), (uint32_t)vma + 4096 * i, true); + return vma; + } + } +} + +#define KPAGE_DIR ((uint32_t *)0x00005000) +#define KPAGE_TABLE_0 ((uint32_t *)0x00400000) + +void init_paging() { + //TODO: use PAE if possible + + for (uint32_t i = 0; i < 1048576; ++i) + KPAGE_TABLE_0[i] = (i * 4096) | PE_WRITABLE | PE_PRESENT; + for (uint16_t i = 0; i < 1024; ++i) + KPAGE_DIR[i] = (uint32_t)(KPAGE_TABLE_0 + i * 1024) | PE_WRITABLE | PE_PRESENT; + + asm volatile ( + "mov $0x00005000, %%eax\n" + "mov %%eax, %%cr3\n" + "mov %%cr0, %%eax\n" + "or $0x80000000, %%eax\n" + "mov %%eax, %%cr0" + : : : "eax"); +} + +void switch_to_kernel_cr3() { + asm volatile ( + "mov $0x00005000, %%eax\n" + "mov %%eax, %%cr3" + : : : "eax"); +} + +void switch_to_task_cr3() { + asm volatile ( + "mov %0, %%cr3" + : : "a" (active_task->page_directory)); +}
\ No newline at end of file diff --git a/src/kernel/paging.h b/src/kernel/paging.h new file mode 100644 index 0000000..72400e8 --- /dev/null +++ b/src/kernel/paging.h @@ -0,0 +1,17 @@ +#include <stdint.h> +#include <stdbool.h> + +void init_paging(); + +void *new_pd(); +void free_pd(void *pd); +void pd_map(void *pd, uint32_t physical_addr, uint32_t virtual_addr, bool writable); +void switch_pd(void *pd); + +void free_task_pd(void *pd); +void *new_task_pd(); +void *pd_user_allocate(void *pd, uint32_t vma, uint32_t pages, bool writable); +void *pd_user_allocate_anywhere_writable(void *pd, uint32_t pages); + +void switch_to_kernel_cr3(); +void switch_to_task_cr3();
\ No newline at end of file diff --git a/src/kernel/pci.c b/src/kernel/pci.c index 28ca6c6..9e33e49 100644 --- a/src/kernel/pci.c +++ b/src/kernel/pci.c @@ -1,7 +1,7 @@ #include "panic.h" #include "boot.h" #include "util.h" -#include "mem.h" +#include "pmap.h" #include "pci.h" enum { @@ -9,7 +9,7 @@ enum { PCP_READ = 0x0cfc }; -uint16_t n_pci_devices = 0; +uint16_t n_pci_devices; static struct pci_device *pci_device_pages[256]; #define PCI_DEVICES_PER_PAGE (4096 / sizeof(struct pci_device)) @@ -31,7 +31,7 @@ struct pci_device *find_pci_device_from_class_and_subclass(uint8_t class, uint8_ static struct pci_device *next_pci_device() { if (!(n_pci_devices % PCI_DEVICES_PER_PAGE)) - if (!(pci_device_pages[n_pci_devices / PCI_DEVICES_PER_PAGE] = allocate_pages(1))) + if (!(pci_device_pages[n_pci_devices / PCI_DEVICES_PER_PAGE] = allocate_kernel_pages(1))) panic("Out of memory in PCI enumeration"); return nth_pci_device(n_pci_devices++); } @@ -68,6 +68,8 @@ void pci_init() { if (!(BOOT_INFO->pci_hw_char & PHC_CS_M1)) panic("No PCI Mechanism 1 support"); + n_pci_devices = 0; + for (uint32_t number = 0; number < (BOOT_INFO->last_pci_bus + 1) * 256; ++number) pci_device_check(number); }
\ No newline at end of file diff --git a/src/kernel/plef.c b/src/kernel/plef.c deleted file mode 100644 index 710943f..0000000 --- a/src/kernel/plef.c +++ /dev/null @@ -1,32 +0,0 @@ -#include <stdint.h> -#include "drive.h" -#include "plef.h" -#include "task.h" -#include "util.h" - -#define PLEF_MAGIC 0xb9ba4c50 - -task_handle plef_run(const struct drive *d, const char *path) { - drive_file_id_t h = d->get_file(d, path); - if (!h) - return 0; - - uint8_t start[512]; - d->load_sector(d, h, 0, start); - - struct plef_header head = *(struct plef_header *)start; - if ((head.magic != PLEF_MAGIC) || (head.version_high)) { - d->free_file(d, h); - return 0; - } - - uint32_t payload_addr; - segment_id cs = new_segment(true, head.payload_length + head.bss_length, &payload_addr); - segment_id ds = mirror_segment(false, cs); - - fmcpy((void *)payload_addr, d, h, head.payload_offset, head.payload_length); - - d->free_file(d, h); - - return new_task(cs, ds, head.entry_point, head.payload_length + head.bss_length); -}
\ No newline at end of file diff --git a/src/kernel/plef.h b/src/kernel/plef.h deleted file mode 100644 index a14ea9b..0000000 --- a/src/kernel/plef.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef PLEF_H -#define PLEF_H - -#include <stdint.h> -#include "drive.h" - -typedef uint8_t task_handle; - -struct plef_header { - uint32_t magic; - uint16_t version_low; - uint16_t version_high; - uint32_t payload_offset; - uint32_t payload_length; - uint32_t bss_length; - uint32_t entry_point; -} __attribute__ ((packed)); - -task_handle plef_run(const struct drive *d, const char *path); - -#endif
\ No newline at end of file diff --git a/src/kernel/pmap.c b/src/kernel/pmap.c new file mode 100644 index 0000000..8634303 --- /dev/null +++ b/src/kernel/pmap.c @@ -0,0 +1,127 @@ +#include <stdint.h> +#include "panic.h" +#include "pmap.h" + +#define PAGEMAP_START (0x00040000) +#define PAGEMAP_END (0x00060000) +#define PAGE_USED(n) ((*(uint8_t *)(PAGEMAP_START + (n >> 3)) >> (n & 7)) & 1) +#define CLEAR_PAGE(n) *(uint8_t *)(PAGEMAP_START + (n >> 3)) &= ~(1 << (n & 7)) +#define SET_PAGE(n) *(uint8_t *)(PAGEMAP_START + (n >> 3)) |= 1 << (n & 7) + +#define KBSS_START (0x04000000) +#define USER_START (0x08000000) +extern const void _kernel_bss_end; + +uint32_t kernel_pages_left; +uint32_t user_pages_left; +uint32_t max_kernel_pages; +uint32_t max_user_pages; + +enum { + BMET_FREE = 1 +}; + +enum { + BMEF_NON_VOLATILE = 0x02 +}; + +struct bios_mmap_entry { + uint64_t base; + uint64_t length; + uint32_t type; + uint32_t flags; +}; + +void init_pagemap() { + for (uint32_t *i = (uint32_t *)(PAGEMAP_START + (KBSS_START >> 15)); i < (uint32_t *)PAGEMAP_END; ++i) + *i = 0xffffffff; + + const struct bios_mmap_entry *mmap_p = (const struct bios_mmap_entry *)0x00010000; + const struct bios_mmap_entry *mmap_e = (const struct bios_mmap_entry *)(0x00010000 + *(uint16_t *)0x00004006); + + for (; mmap_p < mmap_e; ++mmap_p) { + if (mmap_p->type != BMET_FREE) + continue; + if (mmap_p->base > 0xffffffff) + continue; + + uint32_t base_page = ((mmap_p->base - 1) >> 12) + 1; + + uint64_t end = mmap_p->base + mmap_p->length; + if (end > 0xffffffff) + end = 0x100000000; + uint32_t end_page = end >> 12; + + for (uint32_t i = base_page; i < end_page; ++i) + CLEAR_PAGE(i); + } + + uint32_t kernel_pages = (((uint32_t)&_kernel_bss_end - 1) >> 12) + 1; + for (uint32_t i = 0; i < kernel_pages; ++i) + SET_PAGE(i); + + kernel_pages_left = 0; + for (uint32_t i = KBSS_START >> 12; i < USER_START >> 12; ++i) + if (!PAGE_USED(i)) + ++kernel_pages_left; + max_kernel_pages = kernel_pages_left; + + user_pages_left = 0; + for (uint32_t i = USER_START >> 12; i < 1048576; ++i) + if (!PAGE_USED(i)) + ++user_pages_left; + max_user_pages = user_pages_left; +} + +//very inneficient algorithm, just returns first hole big enough. +//a smarter algorithm might pick the smallest one available, +//and go by bytes (or dwords) instead of bits where possible. +void *allocate_kernel_pages(uint32_t n) { + uint32_t run = 0; + + for (uint32_t page = KBSS_START >> 12; page < USER_START >> 12; ++page) { + if (PAGE_USED(page)) + run = 0; + else if (++run == n) { + uint32_t start = page - run + 1; + for (uint32_t i = start; i <= page; ++i) + SET_PAGE(i); + kernel_pages_left -= n; + return (void *)(start << 12); + } + } + + return 0; +} + +//very inneficient algorithm, just returns first hole big enough. +//a smarter algorithm might pick the smallest one available, +//and go by bytes (or dwords) instead of bits where possible. +void *allocate_user_pages(uint32_t n) { + uint32_t run = 0; + + for (uint32_t page = USER_START >> 12; page < 1048576; ++page) { + if (PAGE_USED(page)) + run = 0; + else if (++run == n) { + uint32_t start = page - run + 1; + for (uint32_t i = start; i <= page; ++i) + SET_PAGE(i); + user_pages_left -= n; + return (void *)(start << 12); + } + } + + return 0; +} + +//in the future, change this to go by bytes or dwords instead of bits. +void free_pages(const void *ptr, uint32_t n) { + uint32_t page = (uint32_t)ptr >> 12; + for (uint32_t i = page; i < page + n; ++i) + CLEAR_PAGE(i); + if ((uint32_t)ptr >= USER_START) + user_pages_left += n; + else + kernel_pages_left += n; +}
\ No newline at end of file diff --git a/src/kernel/pmap.h b/src/kernel/pmap.h new file mode 100644 index 0000000..1016de3 --- /dev/null +++ b/src/kernel/pmap.h @@ -0,0 +1,17 @@ +#ifndef PAGEMAP_H +#define PAGEMAP_H + +#include <stdint.h> + +extern uint32_t kernel_pages_left; +extern uint32_t user_pages_left; +extern uint32_t max_user_pages; +extern uint32_t max_kernel_pages; + +void init_pagemap(); + +void *allocate_kernel_pages(uint32_t n) __attribute__ ((malloc)); +void *allocate_user_pages(uint32_t n) __attribute__ ((malloc)); +void free_pages(const void *ptr, uint32_t n); + +#endif
\ No newline at end of file diff --git a/src/kernel/serial.c b/src/kernel/serial.c index 9ca8315..c492e1e 100644 --- a/src/kernel/serial.c +++ b/src/kernel/serial.c @@ -52,9 +52,7 @@ static const uint16_t ports[] = { CP_1, CP_2, CP_3, CP_4 }; -static bool error[] = { - false, false, false, false -}; +static bool error[4]; void reset_error(enum serial_port n) { error[n] = false; @@ -62,6 +60,7 @@ void reset_error(enum serial_port n) { void init_serial() { for (enum serial_port i = COM1; i <= COM4; ++i) { + error[i] = false; outb(ports[i] | CP_INT, 0); outb(ports[i] | CP_LINE, CL_BAUD); outb(ports[i] | CP_DIVLOW, 0x03);//38400 diff --git a/src/kernel/task.c b/src/kernel/task.c index 3e64639..79b2f09 100644 --- a/src/kernel/task.c +++ b/src/kernel/task.c @@ -1,14 +1,92 @@ #include "panic.h" #include "task.h" +#include "paging.h" -segment_id new_segment(bool is_code, uint32_t length, uint32_t *location_out) { - panic("TODO: make new segment"); +struct tss { + struct tss *prev; + + uint32_t esp0; + uint32_t ss0; + uint32_t esp1; + uint32_t ss1; + uint32_t esp2; + uint32_t ss2; + + uint32_t cr3; + uint32_t eip; + uint32_t eflags; + + uint32_t eax; + uint32_t ecx; + uint32_t edx; + uint32_t ebx; + uint32_t esp; + uint32_t ebp; + uint32_t esi; + uint32_t edi; + + uint32_t es; + uint32_t cs; + uint32_t ss; + uint32_t ds; + uint32_t fs; + uint32_t gs; + + uint32_t ldt; + uint16_t trap; + uint16_t iomp; +} __attribute__ ((packed)); + +#define TSS ((struct tss *)0x00004f98) + +#define MAX_TASKS 64 + +struct task_state tasks[MAX_TASKS]; +struct task_state *active_task; + +void init_tasks() { + active_task = tasks; + + for (uint8_t i = 0; i < MAX_TASKS; ++i) + tasks[i].page_directory = 0; + + TSS->ss0 = 0x18; + TSS->esp0 = 0x00040000; +//TSS->cs = 0x13; +//TSS->ds = 0x1b; +//TSS->ss = 0x1b; + TSS->iomp = sizeof(struct tss); + + asm volatile ( + "mov $0x08, %%ax\n" + "ltr %%ax" + : : : "ax"); +} + +void new_task(struct task_state state) { + for (uint8_t n = 0; n < MAX_TASKS; ++n) + if (!tasks[n].page_directory) { + tasks[n] = state; + return; + } + panic("Maximum number of tasks reached."); } -segment_id mirror_segment(bool is_code, segment_id other) { - panic("TODO: make new segment with same base and limit"); +void advance_active_task() { + struct task_state *prev_task = active_task; + do { + if (++active_task == tasks + MAX_TASKS) + active_task = tasks; + if (active_task == prev_task) { + logsz("No active tasks.\nHalting."); + while (1) + asm ("hlt"); + } + } + while (!active_task->page_directory); } -task_handle new_task(segment_id cs, segment_id ds, uint32_t eip, uint32_t esp) { - panic("TODO: add task to scheduler"); +void delete_task(struct task_state *state) { + free_task_pd(state->page_directory); + state->page_directory = 0; }
\ No newline at end of file diff --git a/src/kernel/task.h b/src/kernel/task.h index c3f2acd..838f27b 100644 --- a/src/kernel/task.h +++ b/src/kernel/task.h @@ -4,11 +4,27 @@ #include <stdbool.h> #include <stdint.h> -typedef uint8_t segment_id; -typedef uint8_t task_handle; +struct task_state { + uint32_t ret_addr; + void *page_directory; + //maybe put scheduling or priviledge information here? -segment_id new_segment(bool is_code, uint32_t length, uint32_t *location_out); -segment_id mirror_segment(bool is_code, segment_id other); -task_handle new_task(segment_id cs, segment_id ds, uint32_t eip, uint32_t esp); + uint32_t ebx; + uint32_t ecx; + uint32_t edx; + uint32_t esi; + uint32_t edi; + uint32_t ebp; + uint32_t esp; +} __attribute__ ((packed)); + +extern struct task_state *active_task; + +void init_tasks(); + +void new_task(struct task_state state); +void advance_active_task(); + +void delete_task(struct task_state *state); #endif
\ No newline at end of file diff --git a/src/kernel/util.c b/src/kernel/util.c index 29d7e3f..625622a 100644 --- a/src/kernel/util.c +++ b/src/kernel/util.c @@ -14,7 +14,7 @@ void memcpy(void *to, const void *from, uint32_t n) { *(tpp++) = *(fpp++); } -void fmcpy(void *to, const struct drive *d, drive_file_id_t f, uint32_t from, uint32_t n) { +void fmcpy(void *to, const struct drive *d, file_id_t f, uint32_t from, uint32_t n) { uint8_t buf[512]; d->load_sector(d, f, from >> 9, buf); uint16_t from_low = from & 511; diff --git a/src/kernel/util.h b/src/kernel/util.h index 324f379..9fa8002 100644 --- a/src/kernel/util.h +++ b/src/kernel/util.h @@ -42,7 +42,7 @@ static inline uint32_t ind(uint16_t port) { } void memcpy(void *to, const void *from, uint32_t n); -void fmcpy(void *to, const struct drive *d, drive_file_id_t f, uint32_t from, uint32_t n); +void fmcpy(void *to, const struct drive *d, file_id_t f, uint32_t from, uint32_t n); void u32_dec(uint32_t n, char *b); void u16_dec(uint16_t n, char *b); diff --git a/src/kernel/vga.c b/src/kernel/vga.c index 7506416..c03e5f0 100644 --- a/src/kernel/vga.c +++ b/src/kernel/vga.c @@ -2,9 +2,8 @@ #include <stdint.h> #define VGA_COLUMNS 80 -#define VGA_LINES 25 #define VGA_START (uint16_t *)0x000b8000 -#define VGA_END (VGA_START + VGA_COLUMNS * VGA_LINES) +#define VGA_END (VGA_START + VGA_COLUMNS * 25) static uint16_t *cursor = VGA_START; static uint16_t mask; @@ -12,12 +11,12 @@ void vga_set_color(uint8_t new_color) { mask = new_color << 8; } -void vga_scroll() { +static void vga_scroll() { for (uint32_t *i = (uint32_t *)VGA_START; i < (uint32_t *)(VGA_END - VGA_COLUMNS); ++i) *i = *(i + VGA_COLUMNS / 2); uint32_t f = (mask | (uint8_t)' ') * 0x00010001; for (uint32_t *i = (uint32_t *)(VGA_END - VGA_COLUMNS); i < (uint32_t *)VGA_END; ++i) - *i++ = f; + *i = f; cursor -= VGA_COLUMNS; } @@ -30,12 +29,10 @@ void vga_blank() { } void vga_printch(char ch) { - if (ch == '\n') { - if ((cursor = cursor - (cursor - VGA_START) % VGA_COLUMNS + VGA_COLUMNS) == VGA_END) - vga_scroll(); - return; - } - *cursor++ = mask | (uint8_t)ch; + if (ch == '\n') + cursor = ((cursor - VGA_START) / VGA_COLUMNS + 1) * VGA_COLUMNS + VGA_START; + else + *cursor++ = mask | (uint8_t)ch; if (cursor == VGA_END) vga_scroll(); }
\ No newline at end of file diff --git a/src/kernel/vga.h b/src/kernel/vga.h index 703080f..bfc9fe6 100644 --- a/src/kernel/vga.h +++ b/src/kernel/vga.h @@ -5,7 +5,6 @@ void vga_set_color(uint8_t color); void vga_blank(); -void vga_scroll(); void vga_printch(char ch); #endif
\ No newline at end of file |