XINU
initialize.c
Go to the documentation of this file.
1 
5 #include <xinu.h>
6 #include <string.h>
7 
9 extern void start(void);
11 extern void *_end;
12 
14 extern void main(void);
16 static void sysinit();
18 extern void meminit(void);
20 local process startup(void);
21 
25 struct sentry semtab[NSEM];
27 struct memblk memlist;
28 
30 int prcount;
34 #define CONSOLE_RESET " \033[0m\033[2J\033[;H"
35 
53 void nulluser()
54 {
55  struct memblk *memptr; /* Ptr to memory block */
56  uint32 free_mem; /* Total amount of free memory */
57 
58  /* Initialize the system */
59 
60  sysinit();
61 
62  /* Output Xinu memory layout */
63  free_mem = 0;
64  for (memptr = memlist.mnext; memptr != NULL;
65  memptr = memptr->mnext)
66  {
67  free_mem += memptr->mlength;
68  }
69  kprintf("%10d bytes of free memory. Free list:\n", free_mem);
70  for (memptr = memlist.mnext; memptr != NULL; memptr = memptr->mnext)
71  {
72  kprintf(" [0x%08X to 0x%08X]\n",
73  (uint32)memptr, ((uint32)memptr) + memptr->mlength - 1);
74  }
75 
76  kprintf("%10d bytes of Xinu code.\n",
77  (uint32)&etext - (uint32)&text);
78  kprintf(" [0x%08X to 0x%08X]\n",
79  (uint32)&text, (uint32)&etext - 1);
80  kprintf("%10d bytes of data.\n",
81  (uint32)&ebss - (uint32)&data);
82  kprintf(" [0x%08X to 0x%08X]\n\n",
83  (uint32)&data, (uint32)&ebss - 1);
84 
85  /* Enable interrupts */
86 
87  enable();
88 
89  /* Initialize the network stack and start processes */
90 
91  net_init();
92 
93  /* Create a process to finish startup and start main */
94 
96  "Startup process", 0, NULL));
97 
98  /* Become the Null process (i.e., guarantee that the CPU has */
99  /* something to run when no other process is ready to execute) */
100 
101  while (TRUE)
102  {
103  ; /* Do nothing */
104  }
105 }
106 
115 {
116  uint32 ipaddr; /* Computer's IP address */
117  char str[128]; /* String used to format output */
118 
119  /* Use DHCP to obtain an IP address and format it */
120 
121  ipaddr = getlocalip();
122  if ((int32)ipaddr == SYSERR)
123  {
124  kprintf("Cannot obtain an IP address\n");
125  }
126  else
127  {
128  /* Print the IP in dotted decimal and hex */
129  ipaddr = NetData.ipucast;
130  sprintf(str, "%d.%d.%d.%d",
131  (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
132  (ipaddr >> 8) & 0xff, ipaddr & 0xff);
133 
134  kprintf("Obtained IP address %s (0x%08x)\n", str,
135  ipaddr);
136  }
137  /* Create a process to execute function main() */
138 
139  resume(create((void *)main, INITSTK, INITPRIO,
140  "Main process", 0, NULL));
141 
142  /* Startup process exits at this point */
143 
144  return OK;
145 }
146 
163 static void sysinit()
164 {
165  int32 i;
166  struct procent *prptr; /* Ptr to process table entry */
167  struct sentry *semptr; /* Ptr to semaphore table entry */
168 
170  kprintf("\n%s\n\n", VERSION);
171 
172  /* Platform Specific Initialization */
173 
174  platinit();
175 
176  /* Initialize the interrupt vectors */
177 
178  initevec();
179 
180  /* Initialize free memory list */
181 
182  meminit();
183 
184  /* Initialize system variables */
185 
186  /* Count the Null process as the first process in the system */
187 
188  prcount = 1;
189 
190  /* Scheduling is not currently blocked */
191 
192  Defer.ndefers = 0;
193 
194  /* Initialize process table entries free */
195 
196  for (i = 0; i < NPROC; i++)
197  {
198  prptr = &proctab[i];
199  prptr->prstate = PR_FREE;
200  prptr->prname[0] = NULLCH;
201  prptr->prstkbase = NULL;
202  prptr->prprio = 0;
203  }
204 
205  /* Initialize the Null process entry */
206 
207  prptr = &proctab[NULLPROC];
208  prptr->prstate = PR_CURR;
209  prptr->prprio = 0;
210  strncpy(prptr->prname, "prnull", 7);
211  prptr->prstkbase = getstk(NULLSTK);
212  prptr->prstklen = NULLSTK;
213  prptr->prstkptr = 0;
214  currpid = NULLPROC;
215 
216  /* Initialize semaphores */
217 
218  for (i = 0; i < NSEM; i++)
219  {
220  semptr = &semtab[i];
221  semptr->sstate = S_FREE;
222  semptr->scount = 0;
223  semptr->squeue = newqueue();
224  }
225 
226  /* Initialize buffer pools */
227 
228  bufinit();
229 
230  /* Create a ready list for processes */
231 
232  readylist = newqueue();
233 
234  /* Initialize the real time clock */
235 
236  clkinit();
237 
238  for (i = 0; i < NDEVS; i++)
239  {
240  init(i);
241  }
242  return;
243 }
244 
249 int32 stop(char *s)
250 {
251  kprintf("%s\n", s);
252  kprintf("looping... press reset\n");
253  while (1)
254  /* Empty */;
255 }
256 
262 int32 delay(int n)
263 {
264  DELAY(n);
265  return OK;
266 }
syscall kprintf(char *fmt,...)
ポーリングI/Oを使用して、フォーマットされた文字列をコンソールに出力する。
Definition: kprintf.c:98
#define NULL
連結リスト用のNULLポインタ
Definition: kernel.h:68
uint32 ipucast
Definition: net.h:55
struct defer Defer
defer構造体のextern宣言
Definition: resched.c:8
メモリブロックを管理するための構造体。
Definition: memory.h:53
char * strncpy(char *, const char *, int32)
文字列s1に文字列s2をN文字(Byte)分コピーする。
Definition: strncpy.c:15
static void sysinit()
内部システムの初期化を行うsysinit()のextern宣言
Definition: initialize.c:163
void meminit(void)
フリーメモリリストを初期化するmeminit()のextern宣言
Definition: meminit.c:19
local process startup(void)
スタートアップタスクを完了するプロセス
Definition: initialize.c:114
#define NDEVS
Definition: conf.h:74
byte sstate
エントリ状態が利用可能(S_FREE)か、利用中(S_USED)かを表す。
Definition: semaphore.h:22
struct network NetData
Definition: net.c:6
全てのシステムヘッダファイルをインクルードする。
#define SYSERR
処理が失敗した場合
Definition: kernel.h:79
int prcount
生存しているプロセスの総数
Definition: initialize.c:30
#define DELAY(n)
マイクロ秒単位で処理を遅らせる。
Definition: delay.h:11
セマフォテーブルエントリであり、本構造体の配列(長さNSEM)が静的に確保される。
Definition: semaphore.h:19
#define INITSTK
初期プロセススタックサイズ
Definition: process.h:57
void start(void)
XINUコードの開始であるstart()のextern宣言
struct memblk * mnext
次のフリーメモリブロックへのポインタ
Definition: memory.h:56
struct memblk memlist
フリーメモリブロックのリスト
Definition: initialize.c:27
int ebss
BSSセグメントの終了アドレス(リンカが追加する)
#define OK
処理が成功した場合
Definition: kernel.h:77
#define NSEM
Definition: conf.h:80
int32 sprintf(char *, char *,...)
Definition: sprintf.c:12
int data
データセグメントの開始アドレス(リンカが追加する)
int32 stop(char *s)
処理を停止させる。無限ループによる停止のため、復帰にはリセットが必要である。
Definition: initialize.c:249
int32 initevec(void)
void nulluser()
システムの初期化を処理し、nullプロセスとなる。
Definition: initialize.c:53
#define NULLPROC
NULLプロセスのID。NULLプロセスは、他に動かすプロセスがない時に動く空プロセス
Definition: process.h:54
uint32 prstklen
Bytesで表されたスタックの長さ(最大値。Byte)。
Definition: process.h:96
struct procent proctab[NPROC]
プロセステーブルエントリ
Definition: initialize.c:23
qid16 readylist
READY状態のプロセスリストに対するグローバルID.
Definition: ready.c:9
pri16 prprio
プロセスのスケジューリング優先度。
Definition: process.h:90
void enable(void)
割り込み許可(intr.Sに定義がある)
void net_init(void)
Definition: net.c:15
#define PR_CURR
プロセスが現在動作中。
Definition: process.h:37
void * _end
XINUコードの終わり
int etext
テキストセグメントの終了アドレス(リンカが追加する)
void platinit(void)
Definition: platinit.c:31
#define TRUE
Boolean True(1)
Definition: kernel.h:65
char prname[PNMLEN]
プロセス名。
Definition: process.h:98
#define NPROC
Definition: conf.h:79
#define NULLSTK
NULLプロセスのスタックサイズ
Definition: kernel.h:96
char * prstkbase
ランタイムスタックの基点(メモリ領域で最上位のアドレス)。
Definition: process.h:94
int int32
符号あり32ビット整数(int)
Definition: kernel.h:11
int32 delay(int n)
マイクロ秒単位で処理を遅らせる。
Definition: initialize.c:262
uint16 prstate
プロセス状態(PR_CURR, ..., etc)。
Definition: process.h:88
#define PR_FREE
プロセステーブルエントリが使用されていない状態。
Definition: process.h:35
pri16 resume(pid32)
プロセスを休止状態(サスペンド)からREADY状態に遷移させる。
Definition: resume.c:20
pid32 create(void *, uint32, pri16, char *, uint32,...)
関数の実行を開始するプロセスを作成する。
Definition: create.c:55
uint32 mlength
memblk構造体のサイズを含むブロックサイズ
Definition: memory.h:58
qid16 squeue
セマフォ待機中プロセスのキュー
Definition: semaphore.h:26
status bufinit(void)
バッファプールデータ構造を初期化する。
Definition: bufinit.c:17
#define local
ローカル関数かローカル変数の宣言
Definition: kernel.h:60
uint32 getlocalip(void)
Definition: dhcp.c:142
void main(void)
最初に生成されるmainプロセスのextern宣言
Definition: main.c:18
void clkinit(void)
Definition: clkinit.c:15
int32 ndefers
未処理の延期プロセスの数
Definition: resched.h:19
int32 pid32
プロセスID
Definition: kernel.h:26
pid32 currpid
現在実行中のプロセスのID
Definition: initialize.c:32
qid16 newqueue(void)
グローバルキューテーブルにキューを割り当て、テーブルを初期化する。
Definition: newqueue.c:15
int text
テキストセグメントの開始アドレス(リンカが追加する)
プロセステーブル(32bitsの倍数)。
Definition: process.h:85
unsigned int uint32
符号なし32ビット整数(unsigned int)
Definition: kernel.h:15
#define CONSOLE_RESET
コンソール(コンソールカラー、文字、ポジション)をリセットするためのエスケープシーケンス ...
Definition: initialize.c:34
struct sentry semtab[NSEM]
セマフォテーブルエントリ
Definition: initialize.c:25
int32 scount
セマフォカウント(負の値(-N)の場合は、キューにN個の待機プロセスがある。それ以外はキューが空である) ...
Definition: semaphore.h:24
#define S_FREE
セマフォテーブルエントリが利用可能
Definition: semaphore.h:11
#define NULLCH
NULL文字(NULL終端)
Definition: kernel.h:70
char * getstk(uint32)
スタックメモリを割り当て、最上位のワードアドレスを返す。
Definition: getstk.c:25
#define INITPRIO
初期プロセス優先度
Definition: process.h:59
char * prstkptr
保存されたスタックポインタ。
Definition: process.h:92
syscall init(did32)
デバイスとデバイスドライバを初期化する。
Definition: init.c:17
int32 process
プロセスの最上位レベル関数 返り値の型
Definition: kernel.h:53