blob: ab013fd31fa7d61726b1f062866ddc0c3f5c9034 [file] [log] [blame]
swissChilie4f01992021-02-25 15:38:12 -08001#include "alloc.h"
swissChilid8137922021-02-17 15:34:07 -08002#include "descriptor_tables.h"
swissChili825d46b2021-02-21 10:14:16 -08003#include "io.h"
4#include "log.h"
swissChilicfd3c3c2021-04-03 15:04:24 -07005#include "multiboot.h"
swissChilie4f01992021-02-25 15:38:12 -08006#include "paging.h"
swissChili4749d022021-07-19 12:33:06 -07007#include "sync.h"
swissChilicfd3c3c2021-04-03 15:04:24 -07008#include "syscall.h"
9#include "task.h"
swissChilidefeb0d2021-02-18 15:28:36 -080010#include "timer.h"
swissChilif5448622021-03-08 20:17:36 -080011#include "vfs.h"
swissChilic2e62ed2021-03-10 17:04:18 -080012#include "vfs_initrd.h"
swissChilicfd3c3c2021-04-03 15:04:24 -070013#include "vga.h"
swissChili1a3f07f2021-04-05 20:01:55 -070014#include <dri/ata_pio/ata_pio.h>
swissChili4418ca52021-06-14 17:36:00 -070015#include <dri/fs/ext2/ext2.h>
swissChili4749d022021-07-19 12:33:06 -070016#include <dri/ide/ide.h>
17#include <dri/pci/pci.h>
swissChilicfd3c3c2021-04-03 15:04:24 -070018
19void greet()
20{
swissChili9bd74de2021-06-15 20:30:48 -070021 kprintf(DEBUG "Hello from get_task_id() = %d, get_process_id() = %d\n",
swissChilicfd3c3c2021-04-03 15:04:24 -070022 get_task_id(), get_process_id());
23}
24
swissChilif01ddcc2021-04-04 11:31:34 -070025void other_thread(size_t data)
swissChilicfd3c3c2021-04-03 15:04:24 -070026{
swissChili9bd74de2021-06-15 20:30:48 -070027 kprintf(DEBUG "data is 0x%x\n", data);
swissChilif01ddcc2021-04-04 11:31:34 -070028 greet();
swissChili9bd74de2021-06-15 20:30:48 -070029 kprintf(DEBUG "Returning from other_thread\n");
swissChilif01ddcc2021-04-04 11:31:34 -070030
31 return;
swissChilicfd3c3c2021-04-03 15:04:24 -070032}
swissChilid8137922021-02-17 15:34:07 -080033
swissChilida23a912021-03-12 16:07:50 -080034int kmain(struct multiboot_info *mboot)
swissChilid8137922021-02-17 15:34:07 -080035{
swissChilie0a79bb2021-02-22 19:54:48 -080036 init_paging();
swissChilidc25b2b2021-02-23 17:07:13 -080037 init_vga();
swissChili0b35bf22021-02-18 12:49:40 -080038 init_descriptor_tables();
swissChili64228692021-03-16 08:19:23 -070039 init_syscall();
swissChili6c0519e2021-03-07 19:40:23 -080040 vga_clear();
swissChilid8137922021-02-17 15:34:07 -080041
swissChili825d46b2021-02-21 10:14:16 -080042 init_timer(20);
swissChilie4f01992021-02-25 15:38:12 -080043 init_allocator();
swissChili19ef4182021-02-21 17:45:51 -080044 init_kbd();
swissChilie4f01992021-02-25 15:38:12 -080045
swissChili6c0519e2021-03-07 19:40:23 -080046#ifdef TEST_ALLOC
47 test_allocator();
48#endif
swissChilie4f01992021-02-25 15:38:12 -080049
swissChili9752ab32021-03-05 11:20:13 -080050 // Load initrd
swissChilida23a912021-03-12 16:07:50 -080051 struct multiboot_info mb = make_multiboot_physical(mboot);
swissChili2b5acc82021-03-13 17:06:42 -080052 init_vfs();
swissChili9752ab32021-03-05 11:20:13 -080053
swissChili2b5acc82021-03-13 17:06:42 -080054#ifdef INITRD
swissChili9752ab32021-03-05 11:20:13 -080055 kassert(mb.mods_count, "No multiboot modules loaded!");
swissChili9bd74de2021-06-15 20:30:48 -070056 kprintf(DEBUG "mboot->mods_addr = %d (0x%x)\n", mb.mods_addr, mb.mods_addr);
swissChilida23a912021-03-12 16:07:50 -080057 uchar *initrd_loc = (uchar *)((uint *)mb.mods_addr)[0];
swissChili9752ab32021-03-05 11:20:13 -080058
swissChili9bd74de2021-06-15 20:30:48 -070059 kprintf(DEBUG "initrd is at 0x%x to 0x%x\n", initrd_loc);
swissChili9752ab32021-03-05 11:20:13 -080060
swissChili2b5acc82021-03-13 17:06:42 -080061 init_initrd_vfs(initrd_loc);
62#endif
swissChilida23a912021-03-12 16:07:50 -080063
swissChili9bd74de2021-06-15 20:30:48 -070064 kprintf(OKAY "VFS initialized\n");
swissChilif5448622021-03-08 20:17:36 -080065
swissChili9bd74de2021-06-15 20:30:48 -070066 kprintf(OKAY "Initial setup complete!\n");
swissChilif5448622021-03-08 20:17:36 -080067
swissChili2b5acc82021-03-13 17:06:42 -080068#ifdef TEST_VFS_INITRD
swissChili9bd74de2021-06-15 20:30:48 -070069 kprintf(INFO "fs_readdir(\"/dev/initrd\")\n");
swissChilib7fe8992021-03-10 16:25:47 -080070
71 struct fs_dirent dirent;
swissChilida23a912021-03-12 16:07:50 -080072 for (int i = 0; fs_readdir(&root, i, &dirent); i++)
swissChilib7fe8992021-03-10 16:25:47 -080073 {
swissChili9bd74de2021-06-15 20:30:48 -070074 kprintf(INFO "name: %s, inode: %d\n", dirent.name, dirent.inode);
swissChilib7fe8992021-03-10 16:25:47 -080075 }
swissChili2b5acc82021-03-13 17:06:42 -080076#endif
swissChilib7fe8992021-03-10 16:25:47 -080077
swissChili402a3832021-05-29 21:41:31 -070078 asm("sti");
swissChiliaed6ff32021-05-29 17:51:04 -070079
swissChilicfd3c3c2021-04-03 15:04:24 -070080 init_tasks();
swissChili4749d022021-07-19 12:33:06 -070081 init_sync();
82
swissChili5fe85a12021-05-31 08:10:27 -070083 pci_init();
84
85 // Register PCI drivers
86 ide_register();
87
88 pci_load();
89
swissChilie5adca52021-06-16 21:00:31 -070090 kprintf(OKAY "Loaded PCI device drivers\n");
swissChili9bd74de2021-06-15 20:30:48 -070091
swissChili402a3832021-05-29 21:41:31 -070092#ifdef TEST_THREADS
swissChilif01ddcc2021-04-04 11:31:34 -070093 spawn_thread(other_thread, NULL);
swissChilicfd3c3c2021-04-03 15:04:24 -070094
swissChilif01ddcc2021-04-04 11:31:34 -070095 greet();
swissChili402a3832021-05-29 21:41:31 -070096#endif
swissChilidefeb0d2021-02-18 15:28:36 -080097
swissChiliaed6ff32021-05-29 17:51:04 -070098#ifdef TEST_PCI
swissChili402a3832021-05-29 21:41:31 -070099 pci_print_devices();
swissChili5fe85a12021-05-31 08:10:27 -0700100 pci_print_drivers();
swissChiliaed6ff32021-05-29 17:51:04 -0700101#endif
102
swissChili4418ca52021-06-14 17:36:00 -0700103#ifdef TEST_ATA_PIO
104 test_ata_pio();
105#endif
106
swissChili9bd74de2021-06-15 20:30:48 -0700107 if (ext2_valid_filesystem())
108 {
swissChilib7ef65d2021-07-17 12:51:52 -0700109 kprintf(OKAY "EXT2 filesystem is valid\n");
110 ext2_mount(&root);
swissChili9bd74de2021-06-15 20:30:48 -0700111 }
swissChilib7ef65d2021-07-17 12:51:52 -0700112 else
113 {
swissChili4749d022021-07-19 12:33:06 -0700114 kprintf(
115 WARN
116 "Filesystem is not a valid EXT2 format, only EXT2 is supported\n");
swissChilib7ef65d2021-07-17 12:51:52 -0700117 }
swissChili276b8cf2021-07-16 13:24:42 -0700118
swissChilief97c262021-04-04 10:20:21 -0700119 while (true)
swissChili402a3832021-05-29 21:41:31 -0700120 asm("hlt");
swissChilif01ddcc2021-04-04 11:31:34 -0700121
swissChilid8137922021-02-17 15:34:07 -0800122 return 0xCAFEBABE;
123}