1 /* 2 * Copyright 2008-2010, François Revol, revol@free.fr. All rights reserved. 3 * Copyright 2003-2010, Axel Dörfler, axeld@pinc-software.de. 4 * Distributed under the terms of the MIT License. 5 */ 6 7 8 #include <KernelExport.h> 9 #include <boot/platform.h> 10 #include <boot/heap.h> 11 #include <boot/stage2.h> 12 #include <arch/cpu.h> 13 14 #include <string.h> 15 16 #include "console.h" 17 #include "cpu.h" 18 #include "mmu.h" 19 #include "keyboard.h" 20 #include "toscalls.h" 21 22 23 // GCC defined globals 24 extern void (*__ctor_list)(void); 25 extern void (*__ctor_end)(void); 26 extern uint8 __bss_start; 27 extern uint8 _end; 28 29 extern "C" int main(stage2_args *args); 30 extern "C" void _start(void); 31 32 33 static uint32 sBootOptions; 34 35 36 static void 37 clear_bss(void) 38 { 39 memset(&__bss_start, 0, &_end - &__bss_start); 40 } 41 42 43 static void 44 call_ctors(void) 45 { 46 void (**f)(void); 47 48 for (f = &__ctor_list; f < &__ctor_end; f++) { 49 (**f)(); 50 } 51 } 52 53 54 extern "C" uint32 55 platform_boot_options(void) 56 { 57 #if 0 58 if (!gKernelArgs.fb.enabled) 59 sBootOptions |= check_for_boot_keys(); 60 #endif 61 return sBootOptions; 62 } 63 64 65 extern "C" void 66 platform_start_kernel(void) 67 { 68 static struct kernel_args *args = &gKernelArgs; 69 // something goes wrong when we pass &gKernelArgs directly 70 // to the assembler inline below - might be a bug in GCC 71 // or I don't see something important... 72 addr_t stackTop 73 = gKernelArgs.cpu_kstack[0].start + gKernelArgs.cpu_kstack[0].size; 74 75 preloaded_elf32_image *image = static_cast<preloaded_elf32_image *>( 76 gKernelArgs.kernel_image.Pointer()); 77 78 //smp_init_other_cpus(); 79 //serial_cleanup(); 80 mmu_init_for_kernel(); 81 //smp_boot_other_cpus(); 82 83 #warning M68K: stop ints 84 85 dprintf("kernel entry at %lx\n", image->elf_header.e_entry); 86 87 asm volatile ( 88 "move.l %0,%%sp; " // move stack out of way 89 : : "m" (stackTop)); 90 91 asm volatile ( 92 "or #0x0700,%%sr; " : : ); // disable interrupts 93 94 asm volatile ( 95 "move.l #0x0,-(%%sp); " // we're the BSP cpu (0) 96 "move.l %0,-(%%sp); " // kernel args 97 "move.l #0x0,-(%%sp);" // dummy retval for call to main 98 "move.l %1,-(%%sp); " // this is the start address 99 "rts; " // jump. 100 : : "g" (args), "g" (image->elf_header.e_entry)); 101 102 // Huston, we have a problem! 103 104 asm volatile ( 105 "and #0xf8ff,%%sr; " : : ); // reenable interrupts 106 107 panic("kernel returned!\n"); 108 109 } 110 111 112 extern "C" void 113 platform_exit(void) 114 { 115 // Terminate if running under ARAnyM 116 int32 nfShutdownId = nat_feat_getid("NF_SHUTDOWN"); 117 if (nfShutdownId) 118 nat_feat_call(nfShutdownId, 0); 119 120 // This crashes... 121 // TODO: Puntaes() instead ? 122 Pterm0(); 123 } 124 125 126 extern "C" void 127 _start(void) 128 { 129 stage2_args args; 130 Bconout(DEV_CON, 'H'); 131 132 //asm("cld"); // Ain't nothing but a GCC thang. 133 //asm("fninit"); // initialize floating point unit 134 135 clear_bss(); 136 Bconout(DEV_CON, 'A'); 137 call_ctors(); 138 // call C++ constructors before doing anything else 139 Bconout(DEV_CON, 'I'); 140 141 args.heap_size = 0; 142 args.arguments = NULL; 143 144 // so we can dprintf 145 init_nat_features(); 146 147 //serial_init(); 148 Bconout(DEV_CON, 'K'); 149 console_init(); 150 Bconout(DEV_CON, 'U'); 151 Bconout(DEV_CON, '\n'); 152 dprintf("membot = %p\n", (void*)*TOSVAR_membot); 153 dprintf("memtop = %p\n", (void*)*TOSVAR_memtop); 154 dprintf("v_bas_ad = %p\n", *TOSVAR_v_bas_ad); 155 dprintf("phystop = %p\n", (void*)*TOSVARphystop); 156 dprintf("ramtop = %p\n", (void*)*TOSVARramtop); 157 cpu_init(); 158 mmu_init(); 159 160 // wait a bit to give the user the opportunity to press a key 161 spin(750000); 162 163 // reading the keyboard doesn't seem to work in graphics mode (maybe a bochs problem) 164 sBootOptions = check_for_boot_keys(); 165 //if (sBootOptions & BOOT_OPTION_DEBUG_OUTPUT) 166 //serial_enable(); 167 168 //apm_init(); 169 //smp_init(); 170 main(&args); 171 } 172