-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion
1796 lines (1548 loc) · 51.4 KB
/
version
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* create.c - create, newpid */
#include <conf.h>
#include <kernel.h>
#include <io.h>
#include <proc.h>
#include <dos.h>
#include <mem.h>
typedef int (*fptr)();
#define INITF 0x0200 // initial flag register - set interrupt flag,
// clear direction and trap flags
extern int INITRET(); /* location to return to upon termination */
/*------------------------------------------------------------------------
* create -- create a process to start running a procedure
*------------------------------------------------------------------------
*/
SYSCALL create(procaddr,ssize,priority,namep,nargs,args)
fptr procaddr; // procedure address
word ssize; // stack size in bytes
short priority; // process priority > 0
char *namep; // name (for debugging)
int nargs; // number of args that follow
int args; // arguments (treated like an array)
{
int pid; // stores new process id
struct pentry *pptr;// pointer to proc. table entry
int i; // loop variable
int *currdevs; // current device table
int dev; // device number for stdio
int *a; // points to list of args
char *saddr; // start of stack address
int *sp; // stack pointer
int ps; // saved processor status
int die(); // execute upon process termination
disable(ps);
ssize = roundp(ssize);
if (ssize < MINSTK)
ssize = MINSTK;
if (priority < 1 || (pid=newpid()) == SYSERR ||
((saddr=getstk(ssize)) == NULL ) ) {
restore(ps);
return(SYSERR);
}
numproc++;
pptr = &proctab[pid];
pptr->ssize = ssize;
pptr->pstate = PRSUSP;
pptr->pimmortl = TRUE; // just for the time being...
for (i=0 ; i<PNMLEN ; i++)
pptr->pname[i] = (*namep ? *namep++ : ' ');
pptr->pname[PNMLEN]='\0';
pptr->pprio = priority;
pptr->pwaitret = 0;
pptr->phasmsg = FALSE; // no message
pptr->pimmortl = FALSE; // not immortal
pptr->pnxtkin = BADPID; // no next-of-kin
pptr->pbase = saddr;
pptr->plen = ssize;
pptr->oldtime = 0; // resched hasn't run it yet
pptr->time = 0; // clear CPU time used
pptr->ptfn = die; // initialize trap to die
pptr->ptarg = 0;
pptr->phastrap = FALSE; // no trap yet
currdevs = proctab[currpid].pdevs; // parent device table
/* new process inherits open I/O devices of parent */
for (i=0 ; i<Nsio ; i++) {
dev = open(currdevs[i], NULLPTR);
if (isbaddev(dev))
pptr->pdevs[i] = BADDEV;
else
pptr->pdevs[i] = dev;
}
pptr->pimmortl = FALSE; // back to normal
sp = (int *)(saddr+ssize); // simulate stack pointer
sp -= 16; // forse evita piccoli stack overflow?
pptr->pargs = nargs;
a = (&args) + nargs; // point past last argument
for ( ; nargs > 0 ; nargs--) // machine dependent; copy args
*(--sp) = *(--a); // onto created process' stack
*(--((fptr *) sp)) = INITRET; // push on ret addr
*(--((fptr *) sp)) = procaddr; // simulate a context switch
--sp; // 1 word for bp
*(--sp) = INITF; // FLAGS value
sp -= NSAVEREGS; // words for si, di, ds
pptr->pregs = (char *)sp; // save for context switch
pptr->paddr = procaddr;
restore(ps);
return(pid);
}
/*------------------------------------------------------------------------
* newpid -- obtain a new (free) process id
*------------------------------------------------------------------------
*/
LOCAL newpid()
{
int pid; // process id to return
int i;
for (i=0 ; i<NPROC ; i++) { // check all NPROC slots
if ( (pid=nextproc--) <= 0)
nextproc = NPROC-1;
if (proctab[pid].pstate == PRFREE)
return(pid);
}
return(SYSERR);
}
/* die.c - die */
#include <conf.h>
#include <kernel.h>
#include <proc.h>
#include <sem.h>
#include <dos.h>
#include <io.h>
/*------------------------------------------------------------------------
* die -- terminate the current process and remove it from the system
*------------------------------------------------------------------------
*/
SYSCALL die(pid)
int pid;
{
struct pentry *pptr;
int i;
int ps;
int nok;
/* para xb;
XBLOCK *xbp;
word npara;
word alloc;
*/
disable(ps);
pptr = &proctab[pid]; /* was currpid */
pptr->pprio = MAXINT; /* make sure we go away fast */
pptr->pimmortl = TRUE; /* disable any further kills */
/* close standard devices */
for (i=0 ; i<Nsio ; i++)
close(pptr->pdevs[i]);
/* inform next of kin about our death */
nok = pptr->pnxtkin;
pptr->pnxtkin = BADPID;
if ( nok != pid ) /* was currpid */
send(nok, pid); /* was currpid */
/* free allocated memory */
/* xb = pptr->pmlist;
if (xb == 0)
panic("die: missing stack block");
xbp = (XBLOCK *)memp(xb); // location of stack block
xb = xbp->xbback; // tail of list
xbp->xbback = 0; // make sure chase ends!
alloc = 0;
while (xb != 0) {
xbp = (XBLOCK *)memp(xb);
npara = xbp->xblen;
alloc += npara;
xb = xbp->xbback; // work backwards to stack block
if (freemem((char *)xbp,npara<<4) == SYSERR)
panic("die: unable to free allocated memory");
}
if (alloc != pptr->pmalloc)
panic("die: inconsistent memory allocation");
*/
/* N.B.: avendo rinunciato agli XBLOCK, devo almeno liberare il blocco
* la memoria allocata da \t create() per il processo che ora muore.
* Credo che questo blocco sia concatenato con lo slot pmlist, se si
* usano gli XBLOCK
*/
freestk(pptr->pbase, pptr->plen);
if ( --numproc == 0 )
xdone();
pptr->pstate = PRFREE; /* suicide */
resched();
restore(ps); /* never gets here */
}
/* kill.c - kill */
#include <conf.h>
#include <kernel.h>
#include <proc.h>
#include <sem.h>
#include <dos.h>
/*------------------------------------------------------------------------
* kill -- kill a process and remove it from the system
*------------------------------------------------------------------------
*/
SYSCALL kill(pid)
int pid; /* process to kill */
{
struct pentry *pptr;
int ps;
int die();
disable(ps);
if (isbadpid(pid) || (pptr= &proctab[pid])->pstate==PRFREE) {
restore(ps);
return(SYSERR);
}
if (pid == currpid)
die(pid);
/* if (pptr->pimmortl) {
send(pid, TMSGKILL);
restore(ps);
return(0);
}
*/ switch (pptr->pstate) {
case PRWAIT:
semaph[pptr->psem].semcnt++;
/* fall through */
case PRREADY:
dequeue(pid);
break;
case PRSLEEP:
case PRTRECV:
unsleep(pid);
break;
default:
break;
}
pptr->pprio = MAXINT;
pptr->pstate = PRREADY;
/* Sembra che i processi vengano uccisi facendogli eseguire die()
* attraverso una trap!
* Questa è forse la ragione per cui eliminando il seguito, kill della shell
* non funziona bene, ma la kill originaria dovrebbe funzionare
*/
pptr->ptfn = die;
pptr->ptarg = pid;
pptr->phastrap = TRUE; // essenziale
insert(pid,rdyhead,MAXINT);
resched();
restore(ps);
return(OK);
}
/* xdone.c - xdone */
#include <conf.h>
#include <kernel.h>
#include <proc.h>
#include <io.h>
#include <disk.h>
#include <tty.h>
extern char *memptr;
extern int run;
/*------------------------------------------------------------------------
* xdone -- print system termination message and terminate PC-Xinu
*------------------------------------------------------------------------
*/
int xdone()
{
int kprintf();
int ps, i, ret;
#ifdef Ndsk
for ( i=0; i<Ndsk; i++ )
control(dstab[i].dnum,DSKSYNC); /* sync the disks */
#endif
#ifdef Nsio
sleep(1); /* let tty output settle */
#endif
disable(ps);
#ifdef WINDOWS
scrollup(0,0x184f,0,7); /* clear the screen */
#endif
for ( i=0 ; i<NDEVS ; i++) {
ret = init(i,0); /* un-initialize device */
if (ret != OK)
kprintf("xdone: error un-initializing device %d\n", i);
}
kprintf("\n\n-- system halt --\n\n");
if (numproc==0)
kprintf("All user processes have completed\n");
else
kprintf("terminated with %d process%s active\n",
numproc, ((numproc==1) ? "" : "es"));
kprintf("Returning to DOS . . .\n\n");
disable(ps);
maprestore(); /* restore IRQs */
free(memptr); /* return malloced memory */
run=FALSE; /* kill the NULL process */
return OK;
}
/* panic.c - panic, _panic */
#include <dos.h>
#include <conf.h>
#include <kernel.h>
#include <proc.h>
#include <io.h>
#include <tty.h>
#include <pc.h>
extern int run;
/*------------------------------------------------------------------------
* panic -- print error message and terminate PC-Xinu
*------------------------------------------------------------------------
*/
int panic(cp)
char *cp;
{
int i, pid;
int kprintf();
int ps;
disable(ps);
pid = getpid();
maprestore(); /* restore interrupts to DOS */
for ( i=0 ; i<NDEVS ; i++)
if (init(i,0) != OK)
kprintf("xdone: error un-initializing device %d\n", i);
enable();
kprintf("\n\n-- panic stop (pid=%d) --\n%s\n\n", pid, cp);
if ( numproc == 0 )
kprintf("All user processes have completed\n");
else
kprintf("XINU 7.9 terminated with %d process%s active\n",
numproc, ((numproc==1) ? "" : "es"));
kprintf("Returning . . .\n\n");
run = FALSE;
return SYSERR; /* return to caller */
}
#define MAXSPRINT 6 /* max stack values to print */
static int i; /* counter for printing stack */
static char *ep; /* error message pointer */
static struct pentry *pp; /* pointer to this proc. entry */
static int *sp; /* pointer to original stack */
int *sys_getstk();
/*------------------------------------------------------------------------
* _panic -- exception handler, called by exception interrupts
*------------------------------------------------------------------------
*/
_panic(typ)
int typ; /* exception type */
{
maprestore();
switch (typ) {
case DB0VEC:
ep = "Divide by zero";
break;
case SSTEPVEC:
ep = "Single step";
break;
case BKPTVEC:
ep = "Breakpoint";
break;
case OFLOWVEC:
ep = "Arithmetic overflow";
break;
default:
ep = "Unknown interrupt";
}
kprintf("\n\n-- panic stop -- \n");
kprintf("panic: trap type %d (%s) \n",typ,ep);
pp = &proctab[currpid];
kprintf("process pid=%d name=%s \n",currpid,pp->pname);
sp = sys_getstk(); /* retrieve stack parameters */
sp += 13; /* actual top of user stack */
kprintf("state: ax=%04x bx=%04x cx=%04x dx=%04x",
*(sp-6),*(sp-7),*(sp-8),*(sp-9));
kprintf(" si=%04x di=%04x bp=%04x sp=%04x \n",
*(sp-10),*(sp-11),*(sp-5),FP_OFF(sp));
kprintf(" cs=%04x ds=%04x ss=%04x es=%04x",
*(sp-2),*(sp-12),FP_SEG(sp),*(sp-13));
kprintf(" ip=%04x fl=%04x \n",*(sp-3),*(sp-1));
kprintf("stack dump: \n");
for ( i=0 ; i<MAXSPRINT ; i++, sp++ )
kprintf(" sp=%04x *sp=%04x \n",FP_OFF(sp),*sp);
kprintf("\nPC-Xinu terminated with %d process%s active \n",
numproc, ((numproc==1) ? "" : "es"));
kprintf("Returning . . . \n\n");
halt(); /* return to caller */
}
/* resched.c - resched */
#include <conf.h>
#include <kernel.h>
#include <proc.h>
#include <dos.h>
#include <q.h>
#include <sleep.h>
/*------------------------------------------------------------------------
* resched -- reschedule processor to highest priority ready process
*
* Notes: Upon entry, currpid gives current process id.
* Proctab[currpid].pstate gives correct NEXT state for
* current process if other than PRCURR.
*------------------------------------------------------------------------
*/
int resched()
{
struct pentry *optr; /* pointer to old process entry */
struct pentry *nptr; /* pointer to new process entry */
proctab[currpid].time = proctab[currpid].time + tod
- proctab[currpid].oldtime;
optr = &proctab[currpid];
if ( optr->pstate == PRCURR ) {
/* no switch needed if current prio. higher than next */
if (lastkey(rdytail) < optr->pprio ) {
optr->oldtime = tod;
preempt = 20 /*QUANTUM*/;
return;
}
/* or if rescheduling is disabled ( pcxflag == 0 ) */
if (sys_pcxget() == 0) {
optr->oldtime = tod;
preempt = 1;
return;
}
/* force context switch */
optr->pstate = PRREADY;
insert(currpid,rdyhead,optr->pprio);
}
else if ( sys_pcxget() == 0 ) {
kprintf("pid=%d state=%d name=%s",
currpid,optr->pstate,optr->pname);
panic("Reschedule impossible in this state");
}
/* remove highest priority process at end of ready list */
nptr = &proctab[(currpid=getlast(rdytail))];
nptr->pstate = PRCURR; /* mark it currently running */
// preempt = QUANTUM; /* reset preemption counter */
preempt = 20;
// _pglob = nptr->pglob; /* retrieve global environment */
proctab[currpid].oldtime=tod;
ctxsw(&optr->pregs,&nptr->pregs);
if (currpid != 0 && FP_OFF(optr->pregs) > optr->plen)
panic("stack overflow");
if (optr->phastrap) {
optr->phastrap = FALSE; /* mark trap as serviced */
if (optr->ptfn != NULL)
(*optr->ptfn)(optr->ptarg);
}
/* The OLD process returns here when resumed. */
return;
}
/* resume.c - resume */
#include <conf.h>
#include <kernel.h>
#include <proc.h>
/*------------------------------------------------------------------------
* resume -- unsuspend a process, making it ready; return the priority
*------------------------------------------------------------------------
*/
SYSCALL resume(pid)
int pid;
{
int ps; /* saved processor status */
struct pentry *pptr; /* pointer to proc. tab. entry */
int prio; /* priority to return */
disable(ps);
if (isbadpid(pid) || (pptr = &proctab[pid])->pstate != PRSUSP) {
restore(ps);
return(SYSERR);
}
prio = pptr->pprio;
ready(pid);
resched();
restore(ps);
return(prio);
}
/* sdelete.c - sdelete */
#include <conf.h>
#include <kernel.h>
#include <proc.h>
#include <q.h>
#include <sem.h>
/*------------------------------------------------------------------------
* sdelete -- delete a semaphore by releasing its table entry
*------------------------------------------------------------------------
*/
SYSCALL sdelete(sem)
int sem;
{
int ps;
int pid;
struct sentry *sptr; /* address of sem to free */
int slist;
int freect; /* number of freed processes */
disable(ps);
if ( isbadsem(sem) || semaph[sem].sstate==SFREE ) {
restore(ps);
return(SYSERR);
}
sptr = &semaph[sem];
slist = sptr->sqhead;
sptr->sstate = SFREE;
freect = 0;
while ( (pid=getfirst(slist)) != EMPTY ) {
ready(pid);
freect++;
}
resched();
restore(ps);
return(freect);
}
/* sreset.c - sreset */
#include <conf.h>
#include <kernel.h>
#include <proc.h>
#include <q.h>
#include <sem.h>
/*------------------------------------------------------------------------
* sreset -- reset the count and queue of a semaphore
*------------------------------------------------------------------------
*/
SYSCALL sreset(sem,count)
int sem;
int count;
{
struct sentry *sptr;
int ps;
int pid;
int slist;
int freect; /* number of freed processes */
disable(ps);
if ( isbadsem(sem) || count<0 || semaph[sem].sstate==SFREE ) {
restore(ps);
return(SYSERR);
}
sptr = &semaph[sem];
slist = sptr->sqhead;
sptr->semcnt = count;
freect = 0;
while ( (pid=getfirst(slist)) != EMPTY ) {
ready(pid);
freect++;
}
resched();
restore(ps);
return(freect);
}
/* wait.c - wait */
#include <conf.h>
#include <kernel.h>
#include <proc.h>
#include <q.h>
#include <sem.h>
/*------------------------------------------------------------------------
* wait -- make current process wait on a semaphore
*------------------------------------------------------------------------
*/
SYSCALL wait(sem)
int sem;
{
int ps;
register struct sentry *sptr;
register struct pentry *pptr;
disable(ps);
if (isbadsem(sem) || (sptr = &semaph[sem])->sstate == SFREE) {
restore(ps);
return(SYSERR);
}
if ( --sptr->semcnt < 0 ) {
pptr = &proctab[currpid];
pptr->pstate = PRWAIT;
pptr->psem = sem;
enqueue(currpid,sptr->sqtail);
resched();
}
restore(ps);
return(OK);
}
/* initialize.c - main, sysinit */
#include <dos.h>
#include <conf.h>
#include <kernel.h>
#include <io.h>
#include <proc.h>
#include <sem.h>
#include <q.h>
#include <shell.h>
#include <butler.h>
#include <pc.h>
#include <kbdio.h>
#include <bufpool.h>
#ifdef Ntty
#include <tty.h>
int winofcur; /* define the current input window */
struct tty tty[Ntty]; /* window buffers and mode control */
#endif
#ifdef Nmf
#include <mffile.h>
struct mfblk mftab[Nmf];
#endif
/* Declarations of major kernel variables */
struct pentry proctab[NPROC]; /* process table */
int nextproc; /* next process slot to use in create */
struct sentry semaph[NSEM]; /* semaphore table */
int nextsem; /* next semaphore slot to use in screate*/
struct qent q[NQENT]; /* q table (see queue.c) */
int nextqueue; /* next slot in q structure to use */
MBLOCK memlist; /* list of free memory blocks */
para endaddr; /* beginning of free memory (was para)*/
para maxaddr; /* end of free memory (was para)*/
/* PC-specific variables */
int nmaps; /* no. of active intmap entries */
/* active system state */
int numproc; /* number of live user processes */
int currpid; /* id of currently running process */
char *memptr; /* mallocated memory */
int run; /* TRUE while Xinu is running */
/* real-time clock variables and sleeping process queue pointers */
long tod; /* time-of-day (tics since startup) */
long startup;
int defclk; /* non-zero, then deferring clock count */
int clkdiff; /* deferred clock ticks */
int slnempty; /* FALSE if the sleep queue is empty */
int *sltop; /* address of key part of top entry in */
/* the sleep queue if slnonempty==TRUE */
int clockq; /* head of queue of sleeping processes */
int preempt; /* preemption counter. Current process */
/* is preempted when it reaches zero; */
/* set in resched; counts in ticks */
/* local typedef declaration */
typedef unsigned long big;
/* miscellaneous system variables */
int butlerpid; /* pid of butler process */
int sysmsg; /* message queue */
int rdyhead,rdytail; /* head/tail of ready list (q indexes) */
extern unsigned _stklen;
char *sys_stknpr();
unsigned long curr_sp;
#define NULLNM "**NULL**" /* null process name */
/************************************************************************/
/*** NOTE: ***/
/*** ***/
/*** This is where the system begins after the C environment has ***/
/*** been established. Interrupts are initially DISABLED, and ***/
/*** must eventually be enabled explicitly. This routine turns ***/
/*** itself into the null process after initialization. Because ***/
/*** the null process must always remain ready to run, it cannot ***/
/*** execute code that might cause it to be suspended, wait for a ***/
/*** semaphore, or put to sleep, or exit. In particular, it must ***/
/*** not do I/O unless it uses kprintf for console output. ***/
/*** ***/
/************************************************************************/
/*------------------------------------------------------------------------
* main -- initialize system and become the null process (id==0)
*------------------------------------------------------------------------
*/
main(argc,argv) /* babysit CPU when no one home */
int argc;
char *argv[];
{
int xmain(); /* user's main procedure */
int butler(); /* BUTLER process */
int ps; /* save processor flags */
int pcx; /* reschedule flag */
big maxmem; /* maximum memory location */
big endmem; /* start of free memory */
curr_sp = (unsigned long) sys_stknpr(); /* save current stack pointer */
while ( kbdgetc() != NOCH ) /* eat remaining kbd chars */
;
kprintf("Initializing . . .\n");
disable(ps);
xdisable(pcx);
if ( sysinit() == SYSERR ) { /* initialize all of PC-Xinu */
kprintf("PC-Xinu initialization error\n");
maprestore();
restore(ps);
exit(1);
}
restore(ps);
gettime(&startup);
maxmem = (big)maxaddr << 4;
endmem = (big)endaddr << 4;
kprintf("\nXINU Version %s\n", VERSION);
kprintf("%lu real mem\n",maxmem);
kprintf("%lu base addr\n",endmem);
kprintf("%lu avail mem\n\n", maxmem - endmem);
xrestore(pcx); /* XINU is now running */
run=TRUE;
/* start the butler process */
butlerpid=create(butler,BTLRSTK,BTLRPRIO,BTLRNAME,0);
resume(butlerpid);
/* create the user process */
resume(create(xmain,0x800,INITPRIO,"xmain",3,argc,argv));
while (run) /* run until someone tells us to stop */
pause();
}
#define UL unsigned long
/*------------------------------------------------------------------------
* sysinit -- initialize all PC-Xinu data structures and devices
*------------------------------------------------------------------------
*/
LOCAL sysinit()
{
int i,j;
struct pentry *pptr;
struct sentry *sptr;
struct mblock *mptr;
char *farmalloc(); /* C memory allocator */
int xdone(); /* terminate xinu */
char *namep; /* null process name pointer */
word nblks; /* number of paragraphs to get */
para sizmem; /* memory sizing */
struct devsw *dvptr; /* pointer to devtab entry */
unsigned long avmem;
char *ptr;
int ps;
nmaps=0; /* no. of active intmap entries */
numproc = 0; /* initialize system variables */
nextproc = NPROC-1;
nextsem = NSEM-1;
nextqueue = NPROC; /* q[0..NPROC-1] are processes */
avmem = coreleft(); /* determine bytes of memory left */
memptr=farmalloc(avmem); /* get the lot */
disable(ps); /* farmalloc turns ints on ! */
sizmem=avmem >> 4; /* convert to paragraphs */
endaddr = FP_SEG(memptr); /* start of free memory (para) */
if (FP_OFF(memptr) != 0){
sizmem--;
endaddr++; /* adjust to para boundary */
}
maxaddr = endaddr + sizmem; /* top of free memory (para) */
memlist.mnext = endaddr; /* initialize free memory list */
memlist.mlen = sizmem;
mptr = (MBLOCK *)MK_FP(endaddr,0);
mptr->mnext = 0;
mptr->mlen = sizmem;
for (i=0 ; i<NPROC ; i++) /* initialize process table */
proctab[i].pstate = PRFREE;
ptr = &proctab[0];
for (i=0 ; i<sizeof(struct pentry); i++)
ptr[i]=0;
pptr = &proctab[NULLPROC]; /* initialize null process entry */
pptr->pstate = PRCURR;
pptr->pprio = 0;
pptr->oldtime = 0;
pptr->time = 0;
ptr = sys_stknpr();
pptr->pbase = (char *) (sys_stknpr() - FP_OFF(ptr)); // compute pbase
pptr->pregs = (char *) sys_stknpr(); // current sp
pptr->ssize = 0x1000; // I happen to know this
namep=NULLNM;
for (j=0; j<PNMLEN; j++)
pptr->pname[j] = (*namep ? *namep++ : ' ');
pptr->pname[PNMLEN] = '\0';
pptr->paddr = main;
pptr->pargs = 0;
#ifdef Nsio
/* STDIN, STDOUT, STDERR */
for (i=0 ; i < Nsio ; i++)
pptr->pdevs[i] = (i<3) ? CONSOLE : BADDEV;
#endif
currpid = NULLPROC;
for (i=0 ; i<NSEM ; i++) { /* initialize semaphores */
(sptr = &semaph[i])->sstate = SFREE;
sptr->sqtail = 1 + (sptr->sqhead = newqueue());
}
rdytail=1+(rdyhead=newqueue()); /* initialize ready list */
#ifdef MEMMARK
_mkinit(); /* initialize memory marking */
#else
pinit(); /* initialize ports */
poolinit(); /* initialize the buffer pools */
#endif
clkinit(); /* initialize real-time clock */
#ifdef Ndsk
dskdbp=mkpool(DBUFSIZ, NDBUFF); /* initialize disk buffers */
dskrbp= mkpool(DREQSIZ, NDREQ);
#endif
#ifdef Ntty
winofcur = 0; /* initialize current window */
#endif
/* set up low-level interrupt vectors */
mapinit(DB0VEC, _panic, DB0VEC); /* divide by zero */
mapinit(SSTEPVEC, _panic, SSTEPVEC); /* single step */
mapinit(BKPTVEC, _panic, BKPTVEC); /* breakpoint */
mapinit(OFLOWVEC, _panic, OFLOWVEC); /* overflow */
mapinit(CBRKVEC, cbrkint, CBRKVEC); /* ctrl-break */
#ifdef NDEVS
for ( i=0 ; i<NDEVS ; i++ ) { /* initialize devices */
dvptr = &devtab[i];
if ( dvptr->dvivec && mapinit(dvptr->dvivec,
dvptr->dviint, dvptr->dvminor) == SYSERR )
goto uninit;
if (dvptr->dvovec && mapinit(dvptr->dvovec,
dvptr->dvoint, dvptr->dvminor) == SYSERR )
goto uninit;
if (init(i,1) == OK) /* initialize the device */
continue;
uninit:
kprintf("initiali.c: error initializing device %d\n", i);
while (--i > 0)
init(i,0);
return(SYSERR);
}
#endif
if (mapinit(CLKVEC|BIOSFLG, clkint, 0) == SYSERR)
return(SYSERR);
return(OK);
}
/* getss.c -- get_cur_ss
*
* check if stack saved in sssave/spsave is that of current process
*/
/* ** call only with ints disabled ** */
#include <conf.h>
#include <kernel.h>
#include <proc.h>
#include <dos.h>
extern long sys_getstk();
extern int kprintf();
int get_cur_ss()
{
long curr_stk;
long proc_stk_base;
long proc_stk_top;
int ret;
curr_stk = (long) sys_getstk();
proc_stk_base = (long) proctab[currpid].pbase;
proc_stk_top = (long) proctab[currpid].pbase +
proctab[currpid].ssize;
ret = (curr_stk >= proc_stk_base) && (curr_stk <= proc_stk_top);
#ifdef DEBUG
#define STKMSG "\
Interrupted code stack pointer: %lx\n\
Interrupted process stack base-limit: %lx-%lx\n\n"
if (!ret)
kprintf(STKMSG, curr_stk, proc_stk_base, proc_stk_top);
#endif
return(ret);
}
/* x_butler.c - x_butler */
// Does not appear to work correctly without windows
// but it doesn't for the original XINU 7.9 either!
#include <conf.h>
#include <kernel.h>
#include <proc.h>
#include <dos.h>
#include <q.h>
static char *sttab[8] = {
"???",
"current",
"free",
"ready",
"receiving",
"sleeping ",
"suspended",
"waiting"
};
static struct psnap {
int state;
int prio;
char name[10];
char *pbase;
int plen;
char *pregs;
} stab[NPROC];
static int pmark[NPROC];
static char qname[4];
#define CTRLC 3 /* ASCII code for control-C*/
#define STATE(s) sttab[((s)>0&&(s)<8)?(s):0]
#define next(i) q[(i)].qnext
#define prev(i) q[(i)].qprev
#define key(i) q[(i)].qkey
/*------------------------------------------------------------------------
* x_butler -- (command butler) print process information
*------------------------------------------------------------------------
*/
COMMAND x_butler(nargs,args)
int nargs;
char *args[];
{
int i,j,s;
if ( nargs > 1 ) {
fprintf(STDERR,"Usage: butler\n");
return(SYSERR);
}
pprint();
printf("Press any key to continue . . .");
if ( kgetc(0) == CTRLC )
return(OK);
printf("\n");
for ( i=0; i<NPROC; i++ )
pmark[i] = 0;
/* print the semaphore queues */
i = NPROC;
for ( j=0; j<NSEM; j++ ) {
qname[0] = 'S';
qname[1] = '0'+(j/10);