-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
532 lines (474 loc) · 11.2 KB
/
main.c
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
main.c
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Forrest Yu, 2005
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include "type.h"
#include "stdio.h"
#include "const.h"
#include "protect.h"
#include "string.h"
#include "fs.h"
#include "proc.h"
#include "tty.h"
#include "console.h"
#include "global.h"
#include "proto.h"
PUBLIC int schedule_init()
{
int i;
struct proc *p;
block_queue.start = 0;
block_queue.end = 0;
Ring0_queue.start = 0;
Ring0_queue.end = 0;
task_queue1.start = 0;
task_queue1.end = 0;
task_queue2.start = 0;
task_queue2.end = 0;
task_queue3.start = 0;
task_queue3.end = 0;
task_queue1.give_ticks = 1;
task_queue2.give_ticks = 2;
task_queue3.give_ticks = 4;
LastPriority = 1;
for (i = 0; i < NR_TASKS + NR_NATIVE_PROCS; i++)
{
if (i < NR_TASKS + 1)
{ /* TASK */
p = &FIRST_PROC + i;
p->ticks = p->priority;
}
else if (i < NR_TASKS + 2)
{ /* USER PROC */
task_queue1.proc_pid[task_queue1.start++ % MAX_Queue] = i;
}
} //目前将除测试进程外的进程加入队列
p = &FIRST_PROC + task_queue1.proc_pid[task_queue1.end];
p->ticks = task_queue1.give_ticks;
return 0;
}
void init_sceen()
{
int i;
disp_pos = 0;
for (i = 0; i < 80 * 85; i++)
{
disp_str(" ");
}
disp_pos = 0;
disp_str("Os is init success, beging running\n");
}
/*****************************************************************************
* kernel_main
*****************************************************************************/
/**
* jmp from kernel.asm::_start.
*
*****************************************************************************/
PUBLIC int kernel_main()
{
// disp_str("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
int i, j, eflags, prio;
u8 rpl;
u8 priv; /* privilege */
struct task *t;
struct proc *p = proc_table;
char *stk = task_stack + STACK_SIZE_TOTAL;
for (i = 0; i < NR_TASKS + NR_PROCS; i++, p++, t++)
{
if (i >= NR_TASKS + NR_NATIVE_PROCS)
{
p->p_flags = FREE_SLOT;
continue;
}
if (i < NR_TASKS)
{ /* TASK */
t = task_table + i;
priv = PRIVILEGE_TASK;
rpl = RPL_TASK;
eflags = 0x1202; /* IF=1, IOPL=1, bit 2 is always 1 */
prio = 15;
}
else
{ /* USER PROC */
t = user_proc_table + (i - NR_TASKS);
priv = PRIVILEGE_USER;
rpl = RPL_USER;
eflags = 0x202; /* IF=1, bit 2 is always 1 */
prio = 5;
}
strcpy(p->name, t->name); /* name of the process */
p->p_parent = NO_TASK;
if (i < NR_TASKS + 1) //包含init进程(交互性强,需要内核调度)
{
p->ring = 0;
p->testProc = 0;
p->ticks = prio;
}
else
{
p->ring = 1;
if (i >= NR_TASKS + 2)
{
p->testProc = 1; //这是测试进程
}
else
{
p->testProc = 0;
}
}
if (strcmp(t->name, "INIT") != 0)
{
p->ldts[INDEX_LDT_C] = gdt[SELECTOR_KERNEL_CS >> 3];
p->ldts[INDEX_LDT_RW] = gdt[SELECTOR_KERNEL_DS >> 3];
/* change the DPLs */
p->ldts[INDEX_LDT_C].attr1 = DA_C | priv << 5;
p->ldts[INDEX_LDT_RW].attr1 = DA_DRW | priv << 5;
}
else
{ /* INIT process */
unsigned int k_base;
unsigned int k_limit;
int ret = get_kernel_map(&k_base, &k_limit);
assert(ret == 0);
init_desc(&p->ldts[INDEX_LDT_C],
0, /* bytes before the entry point
* are useless (wasted) for the
* INIT process, doesn't matter
*/
(k_base + k_limit) >> LIMIT_4K_SHIFT,
DA_32 | DA_LIMIT_4K | DA_C | priv << 5);
init_desc(&p->ldts[INDEX_LDT_RW],
0, /* bytes before the entry point
* are useless (wasted) for the
* INIT process, doesn't matter
*/
(k_base + k_limit) >> LIMIT_4K_SHIFT,
DA_32 | DA_LIMIT_4K | DA_DRW | priv << 5);
}
p->regs.cs = INDEX_LDT_C << 3 | SA_TIL | rpl;
p->regs.ds =
p->regs.es =
p->regs.fs =
p->regs.ss = INDEX_LDT_RW << 3 | SA_TIL | rpl;
p->regs.gs = (SELECTOR_KERNEL_GS & SA_RPL_MASK) | rpl;
p->regs.eip = (u32)t->initial_eip;
p->regs.esp = (u32)stk;
p->regs.eflags = eflags;
p->priority = prio;
p->ticks = 0;
p->p_flags = 0;
p->p_msg = 0;
p->p_recvfrom = NO_TASK;
p->p_sendto = NO_TASK;
p->has_int_msg = 0;
p->q_sending = 0;
p->next_sending = 0;
for (j = 0; j < NR_FILES; j++)
p->filp[j] = 0;
stk -= t->stacksize;
}
k_reenter = 0;
ticks = 0;
p_proc_ready = proc_table;
schedule_init();
init_clock();
init_keyboard();
init_sceen();
restart();
while (1)
{
}
}
/*****************************************************************************
* get_ticks
*****************************************************************************/
PUBLIC int get_ticks()
{
MESSAGE msg;
reset_msg(&msg);
msg.type = GET_TICKS;
send_recv(BOTH, TASK_SYS, &msg);
return msg.RETVAL;
}
/**
* @struct posix_tar_header
* Borrowed from GNU `tar'
*/
struct posix_tar_header
{ /* byte offset */
char name[100]; /* 0 */
char mode[8]; /* 100 */
char uid[8]; /* 108 */
char gid[8]; /* 116 */
char size[12]; /* 124 */
char mtime[12]; /* 136 */
char chksum[8]; /* 148 */
char typeflag; /* 156 */
char linkname[100]; /* 157 */
char magic[6]; /* 257 */
char version[2]; /* 263 */
char uname[32]; /* 265 */
char gname[32]; /* 297 */
char devmajor[8]; /* 329 */
char devminor[8]; /* 337 */
char prefix[155]; /* 345 */
/* 500 */
};
/*****************************************************************************
* untar
*****************************************************************************/
/**
* Extract the tar file and store them.
*
* @param filename The tar file.
*****************************************************************************/
void untar(const char *filename)
{
//printf("[extract `%s'\n", filename);
int fd = open(filename, O_RDWR);
assert(fd != -1);
char buf[SECTOR_SIZE * 16];
int chunk = sizeof(buf);
int i = 0;
int bytes = 0;
while (1)
{
bytes = read(fd, buf, SECTOR_SIZE);
assert(bytes == SECTOR_SIZE); /* size of a TAR file
* must be multiple of 512
*/
if (buf[0] == 0)
{
//if (i == 0)
//printf(" need not unpack the file.\n");
break;
}
i++;
struct posix_tar_header *phdr = (struct posix_tar_header *)buf;
/* calculate the file size */
char *p = phdr->size;
int f_len = 0;
while (*p)
f_len = (f_len * 8) + (*p++ - '0'); /* octal */
int bytes_left = f_len;
int fdout = open(phdr->name, O_CREAT | O_RDWR | O_TRUNC);
if (fdout == -1)
{
// printf(" failed to extract file: %s\n", phdr->name);
// printf(" aborted]\n");
close(fd);
return;
}
// printf(" %s\n", phdr->name);
while (bytes_left)
{
int iobytes = min(chunk, bytes_left);
read(fd, buf,
((iobytes - 1) / SECTOR_SIZE + 1) * SECTOR_SIZE);
bytes = write(fdout, buf, iobytes);
assert(bytes == iobytes);
bytes_left -= iobytes;
}
close(fdout);
}
if (i)
{
lseek(fd, 0, SEEK_SET);
buf[0] = 0;
bytes = write(fd, buf, 1);
assert(bytes == 1);
}
close(fd);
//printf(" done, %d files extracted]\n", i);
}
/*****************************************************************************
* shabby_shell
*****************************************************************************/
/**
* A very very simple shell.
*
* @param tty_name TTY file name.
*****************************************************************************/
void shabby_shell(const char *tty_name)
{
int fd_stdin = open(tty_name, O_RDWR);
assert(fd_stdin == 0);
int fd_stdout = open(tty_name, O_RDWR);
assert(fd_stdout == 1);
char rdbuf[128];
while (1)
{
write(1, "$ ", 2);
int r = read(0, rdbuf, 70);
rdbuf[r] = 0;
int argc = 0;
char *argv[PROC_ORIGIN_STACK];
char *p = rdbuf;
char *s;
int word = 0;
char ch;
do
{
ch = *p;
if (*p != ' ' && *p != 0 && !word)
{
s = p;
word = 1;
}
if ((*p == ' ' || *p == 0) && word)
{
word = 0;
argv[argc++] = s;
*p = 0;
}
p++;
} while (ch);
argv[argc] = 0;
int fd = open(argv[0], O_RDWR);
if (fd == -1)
{
if (rdbuf[0])
{
write(1, "{", 1);
write(1, rdbuf, r);
write(1, "}\n", 2);
}
}
else
{
close(fd);
int pid = fork();
if (pid != 0)
{ /* parent */
int s;
wait(&s);
}
else
{ /* child */
execv(argv[0], argv);
}
}
}
close(1);
close(0);
}
/*****************************************************************************
* Init
*****************************************************************************/
/**
* The hen.
*
*****************************************************************************/
void Init()
{
int fd_stdin = open("/dev_tty0", O_RDWR);
assert(fd_stdin == 0);
int fd_stdout = open("/dev_tty0", O_RDWR);
assert(fd_stdout == 1);
printf("Os tty is running success\n");
printf("ALL Os proc is running success\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
/* extract `cmd.tar' */
untar("/cmd.tar");
printf("Now you can enter CTRL+Fn to tty\n");
char *tty_list[] = {"/dev_tty1", "/dev_tty2"};
int i;
for (i = 0; i < sizeof(tty_list) / sizeof(tty_list[0]); i++)
{
int pid = fork();
if (pid != 0)
{ /* parent process */
// printf("[parent is running, child pid:%d]\n", pid);
}
else
{ /* child process */
// printf("[child is running, pid:%d]\n", getpid());
close(fd_stdin);
close(fd_stdout);
shabby_shell(tty_list[i]);
assert(0);
}
}
while (1)
{
int s;
int child = wait(&s);
// printf("child (%d) exited with status: %d.\n", child, s);
}
assert(0);
}
void SYSTEM()
{
while (1)
{
// I don't do nothing now
}
}
/*======================================================================*
TestA
*======================================================================*/
void TestA()
{
while (1)
{
//disp_color_str("hello,I am proc D. now use CPU times: ", BRIGHT | MAKE_COLOR(BLACK, NEW2));
milli_delay(400);
}
}
/*======================================================================*
TestB
*======================================================================*/
void TestB()
{
while (1)
{
milli_delay(400);
}
}
/*======================================================================*
TestC
*======================================================================*/
void TestC()
{
while (1)
{
milli_delay(400);
}
}
void TestD()
{
while (1)
{
milli_delay(400);
}
}
void TestE()
{
while (1)
{
milli_delay(400);
}
}
void TestF()
{
while (1)
{
milli_delay(400);
}
}
/*****************************************************************************
* panic
*****************************************************************************/
PUBLIC void panic(const char *fmt, ...)
{
int i;
char buf[256];
/* 4 is the size of fmt in the stack */
va_list arg = (va_list)((char *)&fmt + 4);
i = vsprintf(buf, fmt, arg);
printl("%c !!panic!! %s", MAG_CH_PANIC, buf);
/* should never arrive here */
__asm__ __volatile__("ud2");
}