stdlibc: \!perror()
[meinos.git] / kernel2 / main.c
blob8c40538267c0feb83b97c95a9348292db15f6965
1 /*
2 meinOS - A unix-like x86 microkernel operating system
3 Copyright (C) 2008 Janosch Gräf <janosch.graef@gmx.net>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <debug.h>
20 #include <stdint.h>
21 #include <multiboot.h>
22 #include <kprint.h>
23 #include <vga.h>
24 #include <syscall.h>
25 #include <paging.h>
26 #include <memmap.h>
27 #include <memphys.h>
28 #include <memkernel.h>
29 #include <gdt.h>
30 #include <idt.h>
31 #include <cpu.h>
32 #include <interrupt.h>
33 #include <lapic.h>
34 #include <tss.h>
35 #include <ipc.h>
36 #include <memuser.h>
37 #include <procm.h>
38 #include <syscall.h>
39 #include <elf.h>
40 #include <string.h>
41 #include <llist.h>
42 #include <rpc.h>
43 #include <signal.h>
44 #include <ioport.h>
45 #include <biosint.h>
46 #include <perm.h>
48 char *basename(char *path) {
49 size_t i;
50 char *basename = path;
51 for (i=0;path[i];i++) {
52 if (path[i]=='/') basename = path+i;
54 if (*basename==0) return basename;
55 else return basename+1;
58 int fourtytwo() {
59 return 42;
62 int putsn(int out,char *buf,size_t maxlen) {
63 size_t i;
64 for (i=0;buf[i]!=0 && i<maxlen;i++) {
65 if (out==0) kprintchar(buf[i]);
66 else com_send(buf[i]);
68 return i;
71 /**
72 * Initializes and runs kernel
73 * @param mbi Multiboot Info
74 * @param magic Multiboot magic number
75 * @return Should not return
77 int main(multiboot_info_t *mbi,uint32_t magic) {
78 vga_init();
80 kprintf("meinOS\n\n");
82 if (magic!=MULTIBOOT_MAGIC) panic("Not booted with a multiboot bootloader.\n");
84 memuser_inited = 0;
85 test(multiboot_init(mbi));
86 test(paging_init());
87 test(memphys_init());
88 test(memkernel_init());
89 test(syscall_init());
90 test(cpu_init());
91 test(gdt_init());
92 test(idt_init());
93 test(interrupt_init());
94 test(lapic_init());
95 test(tss_init());
96 test(ioport_init());
97 test(ipc_init());
98 test(rpc_init());
99 test(signal_init());
100 test(memuser_init());
101 test(proc_init());
102 test(biosint_init());
104 /// @deprecated Only for debugging
105 syscall_create(SYSCALL_PUTSN,putsn,3);
106 syscall_create(SYSCALL_FOURTYTWO,fourtytwo,0);
108 // load initial programs
109 kprintf("Loading initial programs...\n");
110 size_t i,size;
111 void *addr;
112 char *file;
113 char *name;
114 proc_t *proc_init;
116 for (i=0;(addr = multiboot_get_mod(i,&file,&size));i++) {
117 name = basename(file);
118 kprintf(" %s:\t%s:\t0x%x / 0x%x...",name,file,addr,size);
119 proc_t *new = proc_create(name,PERM_ROOTUID,PERM_ROOTGID,i==0?NULL:proc_init,(i==0),(i==0));
120 if (i==0) proc_init = new;
121 if (new!=NULL) {
122 void *entrypoint = elf_load(new->addrspace,addr,size);
123 if (entrypoint!=NULL) {
124 new->registers.eip = (uint32_t)entrypoint;
125 new->registers.esp = (uint32_t)memuser_create_stack(new->addrspace);
126 kprintf("(pid=%d) done\n",new->pid);
127 continue;
129 else proc_destroy(new);
131 kprintf("failed\n");
134 proc_idle();
135 return 0;