-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.asm
More file actions
2055 lines (1633 loc) · 59.3 KB
/
Copy pathkernel.asm
File metadata and controls
2055 lines (1633 loc) · 59.3 KB
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
[BITS 16]
[ORG 0]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Date: 4th July 2009
;; Author: Rahul Ranjan
;; version: 0.1
;; A monolithic 16 bit kernel for x86 PCs
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%define __KERNEL_LOAD_ADDRESS__ 0x07F0
%define __STACK_START_ADDRESS__ 0x07F0
%define __STACK_START_OFFSET__ 0x5000
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This routine calls up different initialization routines, it is called by the bootloader
;;
initKernel:
; cli ; disable interrupts
;; first of all initialize segments
jmp initSegments
INITKERNEL_RET_ADDR_INITSEG:
;; ready to setup system-calls
setUpSysCalls:
call initSysCalls
;; hardware interrupt handler setup.
;; we skip this for now. Without it
;; I/O will not be parallelized.
;; Also we can't implement process
;; switching without it
;; initialise process management
call init_PM
;; initialise memory-management
call init_MM
;; initialize I_O_Manager
call init_I_O_Manager
;; start the scheduler
call init_Scheduler
mov byte[KERNEL_INITIALIZED], 0x01
call setPageColor
;; start the init process
;; init is the first user process
;; all other processes are either
;; started by init or its children
call start_Init
; sti
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Some code for testing
;;
; call setPageColor
; jmp theShell
; mov si, welcome_msg
; xor cx, cx
; mov cl, [welcome_msg_size]
; call printString
;
; int 0x26
;
; call [SYS_CALL_TABLE]
; call [SYS_CALL_TABLE+4]
; call [SYS_CALL_TABLE+8]
; call [SYS_CALL_TABLE+12]
;
; JMP $
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This routine sets up various segments, i.e. DS, ES, SS and also the stack pointers BP and SP
;; Note that we don't set CS since it is already set by the bootloader to the point in memory
;; where kernel code is loaded. This routine should run whenever kernel gets to run.
;;
initSegments:
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
mov ax, __KERNEL_LOAD_ADDRESS__
mov ds, ax
mov es, ax
mov ax, __STACK_START_ADDRESS__
mov ss, ax
mov bp, __STACK_START_OFFSET__
mov sp, bp
cmp byte[KERNEL_INITIALIZED], 0x01
jnz INITKERNEL_RET_ADDR_INITSEG
jmp HANDLER_RET_ADDR_INITSEG
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Kernel Initialization Routine for
;; setting up SYS_CALL_TABLE
;; This should run only once, when the kernel is loaded at boot
;;
initSysCalls:
;; setup the system-call table
%macro setupSysCallTable 2
mov eax, __KERNEL_LOAD_ADDRESS__
shl eax, 4
shl eax, 4
shl eax, 4
shl eax, 4
add eax, %1
mov dword [SYS_CALL_TABLE+4*%2], eax
%endmacro
setupSysCallTable read_SysCall, 0
setupSysCallTable write_SysCall, 1
setupSysCallTable fork_SysCall, 2
setupSysCallTable execve_SysCall, 3
setupSysCallTable write_To_Terminal, 4
;; setup the interrupt vector table, so that 'int 26h' points to the system-call handler
mov bx, 0x0098
mov ax, 0x0000
mov es, ax
mov ecx, __KERNEL_LOAD_ADDRESS__
shl ecx, 4
shl ecx, 4
shl ecx, 4
shl ecx, 4
mov eax, sysCallHandler
add eax, ecx
mov [es:bx], dword eax
ret
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init_PM: initialise process management data structures and routines
;;
init_PM:
pusha
mov ax, word FREE_PCB_LIST_HEAD
mov bx, word[FREE_PCB_LIST_HEAD]
mov di, 0
.loop:
mov word[ds:bx+PCB.prev], ax
mov ax, bx
add ax, word[PCB_SIZE]
mov word[ds:bx+PCB.next], ax
mov ax, bx
mov cx, word[ds:bx+PCB.next]
mov bx, cx
inc di
cmp di, word[MAX_NUM_PCB]
jnz .loop
popa
ret
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init_MM:
;;
init_MM:
mov word[NUM_FREE_MEM_BLOCKS], 240 ;; considering init is loaded at absolute Physical Add 0xCF10 and total available mem in real mode is 1MB
mov word[FREE_MEM_BLOCKS_LIST_HEAD_SEG_NUM], ds
xor ax, ax
add ax, MEM_BLOCK_STRUC_SPACE
mov word[FREE_MEM_BLOCKS_LIST_HEAD_OFF], ax
mov ax, word[FREE_MEM_BLOCKS_LIST_HEAD_SEG_NUM]
mov es, ax
mov bx, word[FREE_MEM_BLOCKS_LIST_HEAD_OFF]
mov di, 0 ; loop counter
.loop:
mov ax, word[USR_PROC_ADD_SPACE_START_SEGMENT_NUM]
mov cx, di
shl cx, 4
shl cx, 4 ; now cx=di*256=di*(4096/16)
add ax, cx ; next segment no
mov word[es:bx+MEM_BLOCK.mem_block_seg_num], ax
mov ax, word[USR_PROC_ADD_SPACE_START_OFFSET] ; offset is always at 0x0000
mov word[es:bx+MEM_BLOCK.mem_block_off], ax
inc di
cmp di, 239
jz .loop_exit
mov word[es:bx+MEM_BLOCK.next_block_seg_num], ds
xor ax, ax
add ax, MEM_BLOCK_STRUC_SPACE
mov cx, di
shl cx, 3
add ax, cx
mov word[es:bx+MEM_BLOCK.next_block_off], ax ; size of one memory block struc is 8 byte
mov ax, word[es:bx+MEM_BLOCK.next_block_seg_num]
mov cx, word[es:bx+MEM_BLOCK.next_block_off]
mov es, ax
mov bx, cx
jmp .loop
.loop_exit:
mov word[es:bx+MEM_BLOCK.next_block_seg_num], 0 ; last block
mov word[es:bx+MEM_BLOCK.next_block_off], 0 ; last block
ret
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init_I_O_Manager:
;;
init_I_O_Manager:
pusha
mov di, 0
.loop:
mov ax, di
mov bl, 120
mul bl
add ax, word FDT_MEM_SPACE
mov bx, word FREE_FDT_LIST
mov word[ds:bx+di], ax
inc di
cmp di, word[MAX_NUM_OF_FDTS]
jnz .loop
popa
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init_Scheduler:
;;
init_Scheduler:
ret
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; start_Init:
;;
start_Init:
;; create PCB for the new process and set current context to it
; mov word[FREE_PCB_LIST_HEAD], PCB_MEM_SPACE
call pcb_Alloc
mov word[ds:bx+PCB.pid], 1
mov word[ds:bx+PCB.ppid], 0
mov byte[ds:bx+PCB.state], 1 ;; state=1 means ready to run, state=0 implies blocked
mov word[CURRENT_PROC_PCB], bx
;; request the memory manager (mem_Alloc routine) for allocation of INIT_PROC_MEM_SIZE bytes
push word 0x0000 ;; this is where mem_Alloc will return offset of pointer to the allocated memory
push word 0x0000 ;; this is where mem_Alloc will return segment num of pointer to the allocated memory
push word 0x1000 ;; no of bytes requested for allocation
mov word[SYS_CALL_ARGS_ADD_SEG], ss
mov word[SYS_CALL_ARGS_ADD_OFF], sp
call mem_Alloc
pop ax
mov ax, 0x1000
mov word[ds:bx+PCB.mem_size], ax
pop ax ;; segment no of allocated memory
mov word[ds:bx+PCB.start_mem_seg], ax
mov word[ds:bx+PCB.start_mem_off], 0 ;; we always keep offset as 0
;; finally load the init program code in the allocated memory and jump to it, ideally scheduler should be run
push word[ds:bx+PCB.start_mem_off]
push word[ds:bx+PCB.start_mem_seg]
push word initProgCode
push ds
push INIT_PROG_SIZE
mov ax, ss
mov es, ax
mov bx, sp
call memcpy
push WORD 0x0CF1
push WORD 0x0000
retf
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Local Routine Data
;;
; INIT_PROC_ADD_SEGMENT_NUM DW 0X17F0
; INIT_PROC_ADD_OFFSET DW 0X0000
INIT_PROC_MEM_SIZE DW 0X1000
cpcount DB 0
INIT_PROG_SIZE DW 300
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;#################################################################################################################################################################;;
;;## ##;;
;;## INITIALISATION DONE, NOW THE REAL KERNEL ROUTINES START ##;;
;;## ##;;
;;#################################################################################################################################################################;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; scheduler: The process scheduler, right now it does nothing
;;
scheduler:
pusha
nop
nop
popa
ret
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; allocate PCB to a new process:
;;
pcb_Alloc:
pusha
cmp word[NUM_FREE_PCB], 0x00
jz ALLOC_FAIL
jmp ALLOC_SCCS
ALLOC_SCCS:
xor bx, bx
xor di, di
mov bx, word[FREE_PCB_LIST_HEAD]
mov di,word[ds:bx+PCB.next]
mov word[FREE_PCB_LIST_HEAD], di
cmp di, 0
jz UPDATE_ALLOCATED_PCB_LIST
mov word[ds:di+PCB.prev], FREE_PCB_LIST_HEAD
UPDATE_ALLOCATED_PCB_LIST:
mov di, word[ALLOCATED_PCB_LIST_HEAD]
mov word[ds:bx+PCB.next], di
mov word[ds:bx+PCB.prev], ALLOCATED_PCB_LIST_HEAD
mov word[ALLOCATED_PCB_LIST_HEAD], bx
cmp di, 0
jz INITIALIZE_PCB
mov word[ds:di+PCB.prev], bx
INITIALIZE_PCB:
mov word[ds:bx+PCB.pid], 0 ;; to be filled by calling routine
mov word[ds:bx+PCB.ppid], 0 ;; to be filled by calling routine
mov byte[ds:bx+PCB.state], 0 ;; state=1 means ready to run, state=0 implies blocked
mov word[ds:bx+PCB.start_mem_seg], 0 ;; to be filled by calling routine
mov word[ds:bx+PCB.start_mem_off], 0 ;; to be filled by calling routine
mov word[ds:bx+PCB.mem_size], 0 ;; to be filled by calling routine
xor ax, ax
mov ax, 1 ; return status of the routine, bx contains the new PCB address offset(wrt DS)
jmp pcb_Alloc.func_ret
ALLOC_FAIL:
xor ax, ax
jmp pcb_Alloc.func_ret
pcb_Alloc.func_ret:
mov word[ret_status], ax
mov word[allocated_pcb_off], bx
popa
mov ax, word[ret_status]
mov bx, word[allocated_pcb_off]
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;
allocated_pcb_off DW 0x0000
ret_status DW 0x0000
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; memory_Manager:
;;
mem_Alloc:
pusha
xor cx, cx
mov bx, word[SYS_CALL_ARGS_ADD_SEG]
mov es, bx
mov bx, word[SYS_CALL_ARGS_ADD_OFF]
mov ax, word[es:bx] ;; amount of memory requested in bytes, RIGHT NOW ITS ALWAYS 4KB
CHECK_AVAILABILITY:
cmp word[NUM_FREE_MEM_BLOCKS],0
jz DENY_ALLOC
jmp ACCEPT_ALLOC
ACCEPT_ALLOC:
dec word[NUM_FREE_MEM_BLOCKS] ;; decrement number of free memory blocks
mov bx, word[FREE_MEM_BLOCKS_LIST_HEAD_SEG_NUM]
mov es, bx
mov bx, word[FREE_MEM_BLOCKS_LIST_HEAD_OFF]
mov ax, word[es:bx+MEM_BLOCK.mem_block_seg_num]
mov cx, word[es:bx+MEM_BLOCK.mem_block_off]
mov bx, word[SYS_CALL_ARGS_ADD_SEG]
mov es, bx ;; load segment no of pointer to return parameters
mov bx, word[SYS_CALL_ARGS_ADD_OFF] ;; load offset of pointer to return parameters
mov word[es:bx], 0x1000 ;; return number of bytes allocated = 4KB
mov word[es:bx+2], ax ;; return segment number of pointer to allocated memory
mov word[es:bx+4], cx ;; return offset of pointer to allocated memory
mov bx, word[FREE_MEM_BLOCKS_LIST_HEAD_SEG_NUM]
mov es, bx
mov bx, word[FREE_MEM_BLOCKS_LIST_HEAD_OFF]
mov ax, word[es:bx+MEM_BLOCK.next_block_seg_num]
mov cx, word[es:bx+MEM_BLOCK.next_block_off]
mov word[FREE_MEM_BLOCKS_LIST_HEAD_SEG_NUM], ax
mov word[FREE_MEM_BLOCKS_LIST_HEAD_OFF], cx
jmp mem_Alloc.func_ret
DENY_ALLOC:
xor ax, ax
mov ax, 0x00 ;; value 0x00 in return status indicates to the calling process that request has been denied
mov es, [SYS_CALL_ARGS_ADD_SEG]
mov bx, [SYS_CALL_ARGS_ADD_OFF]
mov word[es:bx], 0x0000 ;; amount of memory allocated = 0
jmp mem_Alloc.func_ret
mem_Alloc.func_ret:
popa
ret
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Various system-calls
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; syscall_1: read_SysCall
;;
read_SysCall:
call read_Disk ; we should first check the file descriptor table and then decide which routine to call
ret
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; syscall_2: write_SysCall
;;
write_SysCall:
call write2Disk
ret
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;
fork_SysCall:
pusha
;; STEP1 => setup PCB for the new process
call pcb_Alloc
cmp ax, 1
jnz fork_fail
mov word[NEW_PROC_PCB], bx ; save offset of the new PCB in a local var
;; copy the whole PCB Data of the parent process(CURRENT_PROC_PCB) to the child PCB(NEW_PROC_PCB)
push word[NEW_PROC_PCB] ; DST_ADD_OFF
push ds ; DST_ADD_SEG_NUM
push word[CURRENT_PROC_PCB] ; SRC_ADD_OFF
push ds ; SRC_ADD_SEG_NUM
push word[PCB_SIZE] ; NO_OF_BYTES_TO_COPY
mov ax, ss
mov es, ax
mov bx, sp
call memcpy
add sp, 10 ; clean stack
mov bx, word[NEW_PROC_PCB]
mov di, word[CURRENT_PROC_PCB]
mov cx, word[ds:di+PCB.pid]
mov word[ds:bx+PCB.ppid], cx
mov byte[ds:bx+PCB.state], 1 ;; state=1 means ready to run, state=0 implies blocked
mov ax, word[PROCESS_PID_COUNT]
mov word[ds:bx+PCB.pid], ax
cmp word[PROCESS_PID_COUNT], MAX_PID
jz WRAP_PID
inc word[PROCESS_PID_COUNT]
jmp PID_DONE
WRAP_PID:
mov word[PROCESS_PID_COUNT], 2
PID_DONE:
nop
;; request the memory manager (mem_Alloc routine) for allocation of INIT_PROC_MEM_SIZE bytes
push 0x0000 ;; this is where mem_Alloc will return offset of the pointer to the allocated memory
push 0x0000
push word[ds:di+PCB.mem_size] ;; no of bytes requested for allocation is equal to memory size of the parent process,
;; since we are creating a copy
mov word[SYS_CALL_ARGS_ADD_SEG], ss
mov word[SYS_CALL_ARGS_ADD_OFF], sp
call mem_Alloc
pop ax ;; no of bytes allocated
mov word[ds:bx+PCB.mem_size], ax
pop ax ;; segment no of allocated memory
mov word[ds:bx+PCB.start_mem_seg], ax
pop ax
mov word[ds:bx+PCB.start_mem_off], 0 ;; we always keep offset as 0
push word[ds:bx+PCB.start_mem_off]
push word[ds:bx+PCB.start_mem_seg]
push word[ds:di+PCB.start_mem_off]
push word[ds:di+PCB.start_mem_seg]
push word[ds:bx+PCB.mem_size]
mov ax, ss
mov es, ax
mov bx, sp
call memcpy
add sp, 10 ; clean stack
mov bx, word[NEW_PROC_PCB]
mov word[ds:bx+PCB.syscall_ret_value], 0 ;; '0' is returned to the child process
mov ax, word[ds:bx+PCB.pid]
mov word[ds:di+PCB.syscall_ret_value], ax ;; child's PID is returned to the parent
;; now change the child's segment registers, return address's segment value, SS saved on PCB and DS pushed on the stack
mov ax, word[ds:bx+PCB.start_mem_seg] ; this is the segment value for the child's memory space
mov es, ax
mov cx, word[ds:bx+PCB.stack_sp]
mov bx, cx
mov word[es:bx], ax ; change DS of child to the child's memory segment
mov word[es:bx+20], ax ; change segment number of return address
mov bx, word[NEW_PROC_PCB]
mov word[ds:bx+PCB.stack_ss], ax ; finally change the SS on PCB
;; STEP2 => Let's setup the FDT for the new process
mov di, 0
.loop:
mov ax, word[ds:FREE_FDT_LIST+di*2]
cmp ax, 0
jnz .exit_loop
inc di
cmp di, word[MAX_NUM_OF_FDTS]
jnz .loop
.exit_loop:
cmp ax, 0
jz fork_fail
mov bx, word[NEW_PROC_PCB]
mov word[ds:bx+PCB.pointer_to_FDT], ax
;; make the child's FDT, a copy of the parent's FDT
;; our policy is that whatever is open for the parent process
;; is also available to the child process in the same state
mov di, word[CURRENT_PROC_PCB]
mov bx, word[NEW_PROC_PCB]
push word[ds:bx+PCB.pointer_to_FDT]
push ds
push word[ds:di+PCB.pointer_to_FDT]
push ds
push word[FDT_SIZE]
mov ax, ss
mov es, ax
mov bx, sp
call memcpy
add sp, 10 ; clean stack
;; here we are scheduling the child just created for execution,
;; it's temporary hack for scheduling, remove after we have a real working scheduler
mov word[CURRENT_PROC_PCB], bx
jmp _ret
fork_fail:
popa
mov ax, 0
ret
_ret:
popa
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Local Routine Data
;;
MAX_PID DW 65535
PROCESS_PID_COUNT DW 2
NEW_PROC_PCB DW 0
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; not a syscall, helper routine, move somewhere else
;;
memcpy:
;; args to this routine are passed through a pointer to a mem location in the register pair ES:BX
pusha
mov ax, word[es:bx]
mov word[NO_OF_BYTES_TO_COPY], ax
mov ax, word[es:bx+2]
mov word[SRC_ADD_SEG_NUM], ax
mov ax, word[es:bx+4]
mov word[SRC_ADD_OFF], ax
mov ax, word[es:bx+6]
mov word[DST_ADD_SEG_NUM], ax
mov ax, word[es:bx+8]
mov word[DST_ADD_OFF], ax
xor bx, bx
xor ax, ax
xor di, di
_cploop:
mov bx, word[SRC_ADD_SEG_NUM]
mov es, bx
mov bx, word[SRC_ADD_OFF]
xor ax, ax
mov al, byte[es:bx+di]
mov bx, word[DST_ADD_SEG_NUM]
mov es, bx
mov bx, word[DST_ADD_OFF]
mov byte[es:bx+di], al
inc di
cmp di, word[NO_OF_BYTES_TO_COPY]
jnz _cploop
popa
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;
NO_OF_BYTES_TO_COPY DW 0
SRC_ADD_SEG_NUM DW 0
SRC_ADD_OFF DW 0
DST_ADD_SEG_NUM DW 0
DST_ADD_OFF DW 0
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; syscall_4: execve_SysCall
;;
execve_SysCall:
pusha
;; Get the syscall parameters first
mov ax, word[SYS_CALL_ARGS_ADD_SEG]
mov es, ax
mov bx, word[SYS_CALL_ARGS_ADD_OFF]
mov ax, word[es:bx]
mov word[FILE_START_CYL_NUM], ax ; Cyllinder no of the start of the binary file to load
mov ax, word[es:bx+2]
mov word[FILE_START_HEAD_NUM], ax ; Head no of the start of the binary file to load
mov ax, word[es:bx+4]
mov word[FILE_START_SECT_NUM], ax ; Sector no of the start of the binary file to load
mov ax, word[es:bx+6]
mov word[FILE_SIZE], ax ; Size of the binary file in number of sectors
;; now call read_Disk routine (device driver for writing to disk)
;; to load the binary file overwriting calling proc's memory
mov bx, word[CURRENT_PROC_PCB]
push word[ds:bx+PCB.start_mem_off]
push word[ds:bx+PCB.start_mem_seg]
push word[FILE_START_SECT_NUM]
push word[FILE_START_HEAD_NUM]
push word[FILE_START_CYL_NUM]
push word[FILE_SIZE]
mov word[SYS_CALL_ARGS_ADD_SEG], ss
mov word[SYS_CALL_ARGS_ADD_OFF], sp
call read_Disk
add sp, 12 ; cleanup the stack
;; examine the parent's file descriptor's status_flags to decide which ones to preserve
mov bx, word[CURRENT_PROC_PCB]
mov ax, word[ds:bx+PCB.pointer_to_FDT]
mov bx, ax
mov di, 0 ; counter for file descriptor
.loop:
mov al, byte[ds:bx+di+FD.status_flags]
shl al, 4
shl al, 3 ; lowest bit is the flag for CLOSE_ON_EXEC, if set clear this file descriptor
shr al, 3
shr al, 4 ; bring the CLOSE_ON_EXEC flag bit to lowest position, so as to examine it
cmp al, 1 ; check whether CLOSE_ON_EXEC flag is set
jnz _loop_it ; CLOSE_ON_EXEC flag not set, don't close this file
; CLOSE_ON_EXEC flag is set,
; clear this file descriptor
xor ax, ax
mov word[ds:bx+di], ax
mov word[ds:bx+di+2], ax
mov word[ds:bx+di+4], ax
mov word[ds:bx+di+6], ax
mov word[ds:bx+di+8], ax
mov word[ds:bx+di+10], ax
mov byte[ds:bx+di+12], al
_loop_it:
inc di
cmp di, word[MAX_NUM_OF_FDS]
jnz .loop
;; now set the calling proc's return address's offset part to zero, optionally
;; we may also clear the general registers saved on the stack
mov bx, word[CURRENT_PROC_PCB]
mov ax, word[ds:bx+PCB.start_mem_seg]
mov es, ax
mov word[ds:bx+PCB.stack_sp], 0x0FFF ; set the stack pointer so that it points to 0x0fff
sub word[ds:bx+PCB.stack_sp], 24 ; after everything has been popped off
mov word[ds:bx+PCB.stack_bp], 0x0FFF ; also set the BP
mov ax, word[ds:bx+PCB.stack_sp]
mov bx, ax
mov word[es:bx+20], es ; segment of return address, this will be popped into CS
mov word[es:bx+18], 0x0000 ; offset of return address, this will be popped into IP
mov word[es:bx+16], 0x0000 ; rest are just general purpose registers
mov word[es:bx+14], 0x0000
mov word[es:bx+12], 0x0000
mov word[es:bx+10], 0x0000
mov word[es:bx+8], 0x0000
mov word[es:bx+6], 0x0000
mov word[es:bx+4], 0x0000
mov word[es:bx+2], 0x0000
mov word[es:bx], es ; this is where DS register is stored on the stack
popa
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;
FILE_START_CYL_NUM DW 0x0000
FILE_START_HEAD_NUM DW 0x00
FILE_START_SECT_NUM DW 0x00
FILE_SIZE DW 0x0000
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; syscall_5: exit_SysCall
;;
exit_SysCall:
ret
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;syscall_6: malloc_SysCall
;;
malloc_SysCall:
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The system call handler, this is the starting point whenever
;; a system call is made by some user process
sysCallHandler:
; first of all save the calling process general purpose registers
pusha
push ds
; disable interrupts
cli
; set kernel's data segment context
mov cx, __KERNEL_LOAD_ADDRESS__
mov ds, cx
;; save the syscall arguments
mov word[ds:SYS_CALL_NO], ax
mov word[ds:SYS_CALL_ARGS_ADD_OFF], bx
mov word[ds:SYS_CALL_ARGS_ADD_SEG], es
;; save the caller's stack parameters on the PCB
mov bx, word[CURRENT_PROC_PCB]
mov word[ds:bx+PCB.stack_ss], ss
mov word[ds:bx+PCB.stack_sp], sp
mov word[ds:bx+PCB.stack_bp], bp
;; now we call the segments initialization routine
jmp initSegments
HANDLER_RET_ADDR_INITSEG: ;; return address for initSegments
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; JUST FOR TESTING
;;
; call setPageColor
; mov si, sys_call_handler_msg
; xor cx, cx
; mov cx, word[sys_call_handler_msg_size]
; call printString
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Now examine the system call no to decide which routine to call
;; AL register contains the sys-call no, we use it to index into the sys-call table
;; and jmp to the address stored there, since it contains the apropriate routine
;;
xor ax, ax
mov ax, word[ds:SYS_CALL_NO]
call word[ds:SYS_CALL_TABLE + 4 * eax]
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Call the sheduler now
;;
; call scheduler
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; jmp to process runner, to run the scheduled process
;;
jmp run_Proc
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; run_Proc: run the scheduled process
;;
run_Proc:
;; Finally before returning from the interrupt, Restore context of the caller
mov bx, word[CURRENT_PROC_PCB]
mov ss, word[ds:bx+PCB.stack_ss]
mov sp, word[ds:bx+PCB.stack_sp]
mov bp, word[ds:bx+PCB.stack_bp]
; pass the syscall return value into ax register
mov bx, word[CURRENT_PROC_PCB]
mov ax, word[ds:bx+PCB.syscall_ret_value]
mov bx, ss
mov es, bx
mov bx, sp
mov word[es:bx+16], ax
sti
; restore ds register
pop ds
; restore all the general purpose registers
popa
iret
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; read_Disk: device driver for reading data from a disk
;;
read_Disk:
pusha
;; read the system-call arguments
xor ax, ax
mov ax, word[SYS_CALL_ARGS_ADD_SEG]
mov es, ax
mov bx, word[SYS_CALL_ARGS_ADD_OFF]
mov ax, word[es:bx]
mov word[NUM_SECTORS_TO_READ], ax ;; number of sectors to read
mov ax, word[es:bx+2]
mov word[READ_START_AT_CYL_NUM], ax ;; cylinder no can be of 10 bits in the 'int 0x13 BIOS routine'
mov ax, word[es:bx+4]
mov byte[READ_START_AT_HEAD_NUM], al
mov ax, word[es:bx+6]
mov byte[READ_START_AT_SECTOR_NUM], al ;; only 63 sectors are possible, so low 6 bits only are used
mov ax, word[es:bx+8]
mov word[POINTER_TO_BUFFER_SEG_NUM], ax ;; segment no of the address of memory where data has to be stored
mov ax, word[es:bx+10]
mov word[POINTER_TO_BUFFER_OFF], ax ;; offset of the address of memory where data has to be stored
mov ax, word[NUM_SECTORS_TO_READ]
mov word[REMAINING_SECTORS_TO_READ], ax
mov di, 4 ;; set number of retries to 4
READ_SECTORS:
mov ax, word[POINTER_TO_BUFFER_SEG_NUM]
mov es, ax
mov bx, word[POINTER_TO_BUFFER_OFF]
xor ax, ax
mov al, byte[DISK_PARAM_MAX_SECTORS]
sub al, byte[READ_START_AT_SECTOR_NUM] ;; number of sectors to read in the current track
cmp ax, word[REMAINING_SECTORS_TO_READ]
jle READ_CHUNK