summaryrefslogtreecommitdiff
path: root/sos-code-article3/sos/main.c
blob: 3cce9357c47754d52d5150859fd5171301edaa4e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* Copyright (C) 2004  The SOS Team
   Copyright (C) 1999  Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
   USA. 
*/

/* Include definitions of the multiboot standard */
#include <bootstrap/multiboot.h>
#include <hwcore/idt.h>
#include <hwcore/gdt.h>
#include <hwcore/irq.h>
#include <hwcore/exception.h>
#include <hwcore/i8254.h>
#include <sos/list.h>
#include <sos/physmem.h>
#include <sos/klibc.h>
#include <sos/assert.h>
#include <drivers/x86_videomem.h>
#include <drivers/bochs.h>


/* Helper function to display each bits of a 32bits integer on the
   screen as dark or light carrets */
static void display_bits(unsigned char row, unsigned char col,
			 unsigned char attribute,
			 sos_ui32_t integer)
{
  int i;
  /* Scan each bit of the integer, MSb first */
  for (i = 31 ; i >= 0 ; i--)
    {
      /* Test if bit i of 'integer' is set */
      int bit_i = (integer & (1 << i));
      /* Ascii 219 => dark carret, Ascii 177 => light carret */
      unsigned char ascii_code = bit_i?219:177;
      sos_x86_videomem_putchar(row, col++,
			       attribute,
			       ascii_code);
    }
}


/* Clock IRQ handler */
static void clk_it(int intid)
{
  static sos_ui32_t clock_count = 0;

  display_bits(0, 48,
	       SOS_X86_VIDEO_FG_LTGREEN | SOS_X86_VIDEO_BG_BLUE,
	       clock_count);
  clock_count++;

}

#define MY_PPAGE_NUM_INT 511
struct my_ppage
{
  sos_ui32_t before[MY_PPAGE_NUM_INT];
  struct my_ppage *prev, *next;
  sos_ui32_t after[MY_PPAGE_NUM_INT];
}; /* sizeof() Must be <= 4kB */

static void test_physmem()
{
  /* We place the pages we did allocate here */
  struct my_ppage *ppage_list, *my_ppage;
  sos_count_t num_alloc_ppages = 0, num_free_ppages = 0;

  ppage_list = NULL;
  while ((my_ppage = (struct my_ppage*)sos_physmem_ref_physpage_new(FALSE))
	 != NULL)
    {
      int i;
      num_alloc_ppages++;
      
      /* Print the allocation status */
      sos_x86_videomem_printf(2, 0,
			      SOS_X86_VIDEO_FG_YELLOW
			      | SOS_X86_VIDEO_BG_BLUE,
			      "Could allocate %d pages      ",
			      num_alloc_ppages);

      /* We fill this page with its address */
      for (i = 0 ; i < MY_PPAGE_NUM_INT ; i++)
	my_ppage->before[i] = my_ppage->after[i] = (sos_ui32_t)my_ppage;

      /* We add this page at the tail of our list of ppages */
      list_add_tail(ppage_list, my_ppage);
    }

  /* Now we release these pages in FIFO order */
  while ((my_ppage = list_pop_head(ppage_list)) != NULL)
    {
      /* We make sure this page was not overwritten by any unexpected
	 value */
      int i;
      for (i = 0 ; i < MY_PPAGE_NUM_INT ; i++)
	{
	  /* We don't get what we expect ! */
	  if ((my_ppage->before[i] !=  (sos_ui32_t)my_ppage)
	      || (my_ppage->after[i] !=  (sos_ui32_t)my_ppage))
	    {
	      /* STOP ! */
	      sos_x86_videomem_putstring(20, 0,
					 SOS_X86_VIDEO_FG_LTRED
					   | SOS_X86_VIDEO_BG_BLUE,
					 "Page overwritten");
	      return;
	    }
	}

      /* Release the descriptor */
      if (sos_physmem_unref_physpage((sos_paddr_t)my_ppage) < 0)
	{
	  /* STOP ! */
	  sos_x86_videomem_putstring(20, 0,
				     SOS_X86_VIDEO_FG_LTRED
				       | SOS_X86_VIDEO_BG_BLUE,
				     "Cannot release page");
	  return;
	}

      /* Print the deallocation status */
      num_free_ppages ++;
      sos_x86_videomem_printf(2, 0,
			      SOS_X86_VIDEO_FG_YELLOW
			      | SOS_X86_VIDEO_BG_BLUE,
			      "Could free %d pages      ",
			      num_free_ppages);
    }

  /* Print the overall stats */
  sos_x86_videomem_printf(2, 0,
			  SOS_X86_VIDEO_FG_LTGREEN
			  | SOS_X86_VIDEO_BG_BLUE,
			  "Could allocate %d bytes, could free %d bytes     ",
			  num_alloc_ppages << SOS_PAGE_SHIFT,
			  num_free_ppages << SOS_PAGE_SHIFT);

  SOS_ASSERT_FATAL(num_alloc_ppages == num_free_ppages);
}


/* The C entry point of our operating system */
void sos_main(unsigned long magic, unsigned long addr)
{
  unsigned i;
  sos_paddr_t sos_kernel_core_base_paddr, sos_kernel_core_top_paddr;

  /* Grub sends us a structure, called multiboot_info_t with a lot of
     precious informations about the system, see the multiboot
     documentation for more information. */
  multiboot_info_t *mbi;
  mbi = (multiboot_info_t *) addr;

  /* Setup bochs and console, and clear the console */
  sos_bochs_setup();

  sos_x86_videomem_setup();
  sos_x86_videomem_cls(SOS_X86_VIDEO_BG_BLUE);

  /* Greetings from SOS */
  if (magic == MULTIBOOT_BOOTLOADER_MAGIC)
    /* Loaded with Grub */
    sos_x86_videomem_printf(1, 0,
			    SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE,
			    "Welcome From GRUB to %s%c RAM is %dMB (upper mem = 0x%x kB)",
			    "SOS", ',',
			    (unsigned)(mbi->mem_upper >> 10) + 1,
			    (unsigned)mbi->mem_upper);
  else
    /* Not loaded with grub */
    sos_x86_videomem_printf(1, 0,
			    SOS_X86_VIDEO_FG_YELLOW | SOS_X86_VIDEO_BG_BLUE,
			    "Welcome to SOS");

  sos_bochs_putstring("Message in a bochs\n");

  /* Setup CPU segmentation and IRQ subsystem */
  sos_gdt_setup();
  sos_idt_setup();

  /* Setup SOS IRQs and exceptions subsystem */
  sos_exceptions_setup();
  sos_irq_setup();

  /* Configure the timer so as to raise the IRQ0 at a 100Hz rate */
  sos_i8254_set_frequency(100);


  /* We need a multiboot-compliant boot loader to get the size of the RAM */
  if (magic != MULTIBOOT_BOOTLOADER_MAGIC)
    {
      sos_x86_videomem_putstring(20, 0,
				 SOS_X86_VIDEO_FG_LTRED
				   | SOS_X86_VIDEO_BG_BLUE
				   | SOS_X86_VIDEO_FG_BLINKING,
				 "I'm not loaded with Grub !");
      /* STOP ! */
      for (;;)
	continue;
    }

  /* Binding some HW interrupts and exceptions to software routines */
  sos_irq_set_routine(SOS_IRQ_TIMER,
			    clk_it);
  /* Enabling the HW interrupts here, this will make the timer HW
     interrupt call our clk_it handler */
  asm volatile ("sti\n");
  /* Multiboot says: "The value returned for upper memory is maximally
     the address of the first upper memory hole minus 1 megabyte.". It
     also adds: "It is not guaranteed to be this value." aka "YMMV" ;) */
  sos_physmem_setup((mbi->mem_upper<<10) + (1<<20),
		    & sos_kernel_core_base_paddr,
		    & sos_kernel_core_top_paddr);
  test_physmem();

  /* An operatig system never ends */
  for (;;)
    continue;

  return;
}