Modestus Moon OS  R4
CS 450 project
kmain.c
Go to the documentation of this file.
1 /*
2  ----- kmain.c -----
3 
4  Description..: Kernel main. The first function called after
5  the bootloader. Initialization of hardware, system
6  structures, devices, and initial processes happens here.
7 */
8 
9 #include <stdint.h>
10 #include <string.h>
11 #include <system.h>
12 #include <core/rtc.h>
13 
14 #include <core/io.h>
15 #include <core/serial.h>
16 #include <core/tables.h>
17 #include <core/interrupts.h>
18 #include <core/anim.h>
19 #include <mem/heap.h>
20 #include <mem/paging.h>
21 #include <core/commhand.h>
22 
23 #include <core/pcb.h>
24 #include <core/mcb.h>
25 
26 #include "linked_list.h"
27 
28 #include "core/mpx_supt.h"
29 
30 void kmain(void)
31 {
32  extern uint32_t magic;
33  // Uncomment if you want to access the multiboot header
34  // extern void *mbd;
35  // char *boot_loader_name = (char*)((long*)mbd)[16];
36 
37  // 0) Initialize Serial I/O and call mpx_init
38  klogv("Starting MPX boot sequence...");
39  klogv("Initialized serial I/O on COM1 device...");
40 
41 // nick was here 1/19/17 and added more text
42 //Gio was also here on 1/20/2017
43 
44  //Added: matt-g 1/17/17
47 
48  start_up_anim();
49 
50  //added matt-g 1/25/17
51 // char buf[500];
52 // char *testarr = (char*)buf;
53 /*
54  const char* toCpy = "testetst";
55  testarr=strcat(testarr,toCpy);
56  printf("-%s-%s %s %d %s %c %s","testtest","matt","jesse",-938,"gio",'l',"nick");
57  printf("atoi test: %d\r\n", atoi("12 4"));
58  //klogv(testarr);
59 
60  int seconds = 30, minutes=49, hours=5;
61  get_time(&hours,&minutes,&seconds);
62  sprintf(&testarr[0], 500, "Hours %d, Minutes %d, Seconds %d", hours, minutes, seconds);
63  klogv(testarr);
64 
65  int day=25, month=12, year=2016;
66  get_date(&day, &month, &year);
67  sprintf(&testarr[0], 500, "Day %d, Month %d, Year %d", day, month, year);
68  klogv(testarr);
69 */
70  // 1) Check that the boot was successful and correct when using grub
71  // Comment this when booting the kernel directly using QEMU, etc.
72  if ( magic != 0x2BADB002 ){
73  //kpanic("Boot was not error free. Halting.");
74  }
75 
76 
77 
78  // 2) Descriptor Tables
79  klogv("Initializing Global descriptor table...");
80  init_gdt(); //Added: matt-g 17/1/18
81 
82  klogv("Initializing Interrupt Descriptor table..");
83  init_idt();
84 
85  klogv("Initializing Programmable interrupt controller");
86  init_pic();
87 
90  initHeap(50000); //init heap
91  //heapTest();
92 
93  klogv("Installing interrupts...");
94  init_irq();
95 
96  klogv("Enabling interrupts.");
97  cli();
98 
99  // 4) Virtual Memory
100  klogv("Initializing virtual memory...");
101  init_paging();
102 
103  klogv("Initializing PCB Memory");
104  initPCBQueues();
105 
106  // 5) Call Commhand
107  klogv("Transferring control to commhand...");
108 
109  //listTest();
110 
111  //printf("%s%s","\r\n","\r\n");
112 
113  //pcbTest();
114 
115  pcb_t* newPCB = setupPCB("Commhand", SYSTEM, 9);
116  processContext_t * cp = ( newPCB->context );
117  memset ( cp , 0, sizeof ( processContext_t ));
118  cp->fs = 0x10 ;
119  cp->gs = 0x10 ;
120  cp->ds = 0x10 ;
121  cp->es = 0x10 ;
122  cp->cs = 0x8 ;
123  cp->ebp = ( u32int )( newPCB->stackBase );
124  cp->esp = ( u32int )( newPCB->stackTop );
125  cp->eip = ( u32int ) &init_commhand;
126  cp->eflags = 0x202 ;
127 
129 
130  newPCB = setupPCB("IDLE", SYSTEM, 0);
131  cp = ( newPCB->context );
132  memset ( cp , 0, sizeof ( processContext_t ));
133  cp->fs = 0x10 ;
134  cp->gs = 0x10 ;
135  cp->ds = 0x10 ;
136  cp->es = 0x10 ;
137  cp->cs = 0x8 ;
138  cp->ebp = ( u32int )( newPCB->stackBase );
139  cp->eip = ( u32int ) &idle;
140  cp->eflags = 0x202 ;
141 
143 
144  sys_req(IDLE);
145 
146  // 11) System Shutdown
147  klogv("Starting system shutdown procedure...");
148 
149  /* Shutdown Procedure */
150  klogv("Shutdown complete. You may now turn off the machine. (QEMU: C-a x)");
151  hlt();
152 }
u32int eflags
Definition: system.h:75
void klogv(const char *msg)
Definition: system.c:11
u32int gs
Definition: system.h:73
int sys_req(int op_code)
Definition: mpx_supt.c:11
void init_gdt()
Definition: tables.c:75
int set_serial_out(int device)
Definition: serial.c:75
void initPCBQueues(void)
initPCBQueues initlize the queues for each process queue. This function calls the list init function ...
Definition: pcb.c:233
The s_processContext struct defines the context that each process stores .
Definition: system.h:71
void sys_set_malloc(void *(*func)(size_t))
Definition: mpx_supt.c:23
void start_up_anim(void)
Definition: anim.c:21
u32int esp
Definition: system.h:74
void init_paging()
Definition: paging.c:111
void initHeap(size_t initialHeapSize)
initHeap initializes the system heap by calling kmalloc with a valid byte size. Creates a CMCB at the...
Definition: mcb.c:11
#define hlt()
Definition: system.h:17
void init_irq(void)
Definition: interrupts.c:68
#define cli()
Definition: system.h:15
u32int fs
Definition: system.h:73
typedef for pcb_t struct
Definition: pcb.h:54
void kmain(void)
Definition: kmain.c:30
int set_serial_in(int device)
Definition: serial.c:87
void init_pic(void)
Definition: interrupts.c:109
u32int es
Definition: system.h:73
#define COM1
Definition: serial.h:6
unsigned long uint32_t
Definition: system.h:31
u32int cs
Definition: system.h:75
void sys_set_free(int(*func)(void *))
Definition: mpx_supt.c:28
u32int ebp
Definition: system.h:74
pcb_t * setupPCB(const char *processName, e_PROCESS_CLASS_t processClass, processPriority_t processPriority)
setupPCB calls allocatePCB, initializes the PCB with the arguments and sets it state to ready ...
Definition: pcb.c:71
e_PCB_ERROR_CODE_t changeProcessSuspensionState(const char *processName, e_PROCESS_SUSPENSION_STATE_t suspensionState)
changeProcessSuspensionState
Definition: pcb.c:190
void init_commhand(void)
init_commhand Starts input loop and waits for shutdown code
Definition: commhand.c:200
unsigned long u32int
Definition: system.h:27
#define IDLE
Definition: mpx_supt.h:7
void * allocateMemFromHeap(size_t requestedSize)
allocateMemFromHeap takes the requested size and finds the first block that fits. Implements first fi...
Definition: mcb.c:45
void * memset(void *s, int c, size_t n)
memset Set a region of memory.
Definition: string.c:139
int freeHeapMem(void *blockAddress)
freeHeapMem takes the block address and frees the memory from the CMCB start to the LMCB end location...
Definition: mcb.c:154
u32int eip
Definition: system.h:75
void init_idt()
Definition: tables.c:43
void idle()
Definition: mpx_supt.c:49
u32int ds
Definition: system.h:73