aboutsummaryrefslogtreecommitdiffstats
path: root/src/syscall.asm
blob: 870a2ff594011071abbba0eff6a74e9fee002fd8 (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
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
; =============================================================================
; syscall.asm - the trap entry, dispatch table, and handlers.
;
; ABI:  user loads C = syscall number, args in DE/B/HL, then `rst $30`.
;       return value in A.  ($30 vector jp's here - see boot.asm)
; =============================================================================
INCLUDE "include/gbos.inc"

SECTION "syscall", ROM0

; -----------------------------------------------------------------------------
SyscallTrap::
    ld a, c
    cp SYS_MAX
    jr nc, .bad
    ; index the table WITHOUT touching DE/B (those are syscall args).
    ld hl, SyscallTable
    ld a, c
    add a                       ; A = C*2 (word entries; C < SYS_MAX)
    add l
    ld l, a
    jr nc, .nocarry
    inc h
.nocarry
    ld a, [hl+]
    ld h, [hl]
    ld l, a
    jp hl                       ; tail-call; handler's `ret` returns to user
.bad
    ld a, $FF                   ; ENOSYS-ish
    ret

; -----------------------------------------------------------------------------
; Dispatch table (indexed by syscall number)
; -----------------------------------------------------------------------------
SyscallTable:
    dw sys_exit                 ; 0
    dw sys_fork                 ; 1  FORK
    dw sys_read                 ; 2  READ
    dw sys_write                ; 3  WRITE
    dw sys_open                 ; 4  OPEN
    dw sys_close                ; 5  CLOSE
    dw sys_exec                 ; 6  EXEC
    dw sys_wait                 ; 7  WAIT
    dw sys_getpid               ; 8  GETPID
    dw sys_kill                 ; 9  KILL
    dw sys_nosys                ; 10 BRK   (TODO)
    dw sys_yield                ; 11 YIELD
    dw sys_getb                 ; 12 GETB
    dw sys_putb                 ; 13 PUTB
    dw sys_list                 ; 14 LIST
    dw sys_remove               ; 15 REMOVE
    dw sys_putc                 ; 16 PUTC
    dw sys_setin                ; 17 SETIN
    dw sys_setout               ; 18 SETOUT
    dw sys_lookup               ; 19 LOOKUP
    dw sys_ps                   ; 20 PS
    dw sys_progname             ; 21 PROGNAME
    dw sys_reap                 ; 22 REAP
    dw sys_bfill                ; 23 BFILL (debug)
    dw sys_bpeek                ; 24 BPEEK (debug)
    dw sys_mkdir                ; 25 MKDIR
    dw sys_chdir                ; 26 CHDIR
    dw sys_opendir              ; 27 OPENDIR

; -----------------------------------------------------------------------------
sys_nosys:
    ld a, $FF
    ret

; -----------------------------------------------------------------------------
; sys_write(fd=B, buf=DE, len=B?) -- scaffold: fd ignored, DE=buf, B=len.
;   Writes to the serial console. Returns A = bytes written.
; -----------------------------------------------------------------------------
sys_write:
    ld a, b
    or a
    ret z                       ; len 0
.loop
    ld a, [de]
    push de
    push bc
    ld e, a
    call KPutc                  ; route the byte to this process's stdout
    pop bc
    pop de
    inc de
    dec b
    jr nz, .loop
    ret

; -----------------------------------------------------------------------------
; CurStdin / CurStdout -> A = current process PROC_STDIN / PROC_STDOUT
; -----------------------------------------------------------------------------
CurStdin:
    ld a, [wCurProc]
    add PROC_STDIN
    ld l, a
    ld a, [wCurProc+1]
    adc 0
    ld h, a
    ld a, [hl]
    ret
CurStdout:
    ld a, [wCurProc]
    add PROC_STDOUT
    ld l, a
    ld a, [wCurProc+1]
    adc 0
    ld h, a
    ld a, [hl]
    ret

; -----------------------------------------------------------------------------
; KGetc -> A = byte, CF set on EOF. Reads this process's stdin (console/file).
; -----------------------------------------------------------------------------
KGetc::
    call CurStdin
    cp STD_CONSOLE
    jr z, .console
    ld b, a
    jp sys_getb                 ; B=fd -> A=byte, CF=EOF
.console
.poll
    ld a, $80
    ld [rSC], a
    ld a, [rSC]
    bit 7, a
    jr z, .got
    call SchedYield
    jr .poll
.got
    ld a, [rSB]
    and a                       ; CF = 0
    ret

; -----------------------------------------------------------------------------
; KPutc(E = byte) - write to this process's stdout (console/file).
; -----------------------------------------------------------------------------
KPutc::
    ld a, e
    ld [wKChar], a
    call CurStdout
    cp STD_CONSOLE
    jr z, .console
    ld b, a                     ; fd
    ld a, [wKChar]
    ld e, a
    jp sys_putb                 ; B=fd, E=byte
.console
    ld a, [wKChar]
    ld [rSB], a                 ; mirror to serial (test capture / link)
    ld a, $81
    ld [rSC], a
    ld a, [wKChar]
    call term_putc              ; and render on the LCD terminal
    ret

; -----------------------------------------------------------------------------
; sys_putc(E = byte), sys_setin/sys_setout(B = fd)
; -----------------------------------------------------------------------------
sys_putc:
    call KPutc
    ret
sys_setin:
    ld a, [wCurProc]
    add PROC_STDIN
    ld l, a
    ld a, [wCurProc+1]
    adc 0
    ld h, a
    ld a, b
    ld [hl], a
    ret
sys_setout:
    ld a, [wCurProc]
    add PROC_STDOUT
    ld l, a
    ld a, [wCurProc+1]
    adc 0
    ld h, a
    ld a, b
    ld [hl], a
    ret

; -----------------------------------------------------------------------------
; sys_lookup(DE = name) -> A = program id, or $FF if not a known command.
; -----------------------------------------------------------------------------
sys_lookup:
    ld h, d
    ld l, e                     ; HL = query name
    ld de, NameTable
.l
    ld a, [de]
    or a
    jr z, .none
    push hl
    push de
    call StrEqual               ; HL=query vs DE=table name -> Z if equal
    pop de
    pop hl
    jr z, .match
    call SkipName               ; DE -> NUL
    inc de                      ; -> id
    inc de                      ; -> next entry
    jr .l
.match
    call SkipName
    inc de                      ; -> id
    ld a, [de]
    ret
.none
    ld a, $FF
    ret

; -----------------------------------------------------------------------------
; sys_read() -> A = one input byte from the console (blocking).
; Uses an external-clock serial transfer as a clean RX: it completes only when
; the host has a byte (no stdout echo). While waiting we yield so other procs
; run. Preserves BC/DE/HL (the context switch saves/restores them).
; -----------------------------------------------------------------------------
sys_read:
    call KGetc                  ; A=byte, CF=EOF (file); console EOF is $04
    ret nc
    ld a, $04                   ; file EOF -> EOT
    ret

; -----------------------------------------------------------------------------
; sys_getpid() -> A = current pid
; -----------------------------------------------------------------------------
sys_getpid:
    ld a, [wCurProc]
    add PROC_PID
    ld l, a
    ld a, [wCurProc+1]
    adc 0
    ld h, a
    ld a, [hl]
    ret

; -----------------------------------------------------------------------------
; sys_yield()
; -----------------------------------------------------------------------------
sys_yield:
    call SchedYield
    ret

; -----------------------------------------------------------------------------
; sys_exit(code=B) -- become a zombie awaiting reap; never returns.
;   - store exit code, set PS_ZOMBIE
;   - reparent any children to init (so they can still be waited on)
;   - wake our parent if it is blocked in wait()
;   - schedule away forever (RAM bank + PCB slot are freed by the reaper)
; -----------------------------------------------------------------------------
sys_exit:
    ; PROC_STATE = PS_ZOMBIE
    ld a, [wCurProc]
    ld l, a
    ld a, [wCurProc+1]
    ld h, a
    ld a, PS_ZOMBIE
    ld [hl], a
    ; PROC_EXIT = B
    ld a, [wCurProc]
    add PROC_EXIT
    ld l, a
    ld a, [wCurProc+1]
    adc 0
    ld h, a
    ld a, b
    ld [hl], a
    ; stash my pid + parent pid
    ld a, [wCurProc]
    add PROC_PID
    ld l, a
    ld a, [wCurProc+1]
    adc 0
    ld h, a
    ld a, [hl]
    ld [wExitMyPid], a
    ld a, [wCurProc]
    add PROC_PARENT
    ld l, a
    ld a, [wCurProc+1]
    adc 0
    ld h, a
    ld a, [hl]
    ld [wExitParentPid], a
    ; hand any children to init
    call ReparentToInit
    ; wake our parent if it's blocked in wait()
    ld a, [wExitParentPid]
    call FindPcbByPid           ; DE=&parent PCB, CF if gone
    jr c, .gone
    ld a, [de]
    cp PS_BLOCKED
    jr nz, .gone
    ld a, PS_READY
    ld [de], a
.gone
    ; never run this process again
.dead
    call SchedYield
    jr .dead

; -----------------------------------------------------------------------------
; sys_wait() -- reap a zombie child.
;   out: A = child pid, B = exit code; or A = $FF if we have no children.
;   Blocks (PS_BLOCKED) until a child becomes a zombie.
; -----------------------------------------------------------------------------
sys_wait:
    ld a, [wCurProc]
    add PROC_PID
    ld l, a
    ld a, [wCurProc+1]
    adc 0
    ld h, a
    ld a, [hl]
    ld [wWaitMyPid], a
.retry
    ld c, 0                     ; slot cursor
    ld b, 0                     ; "any child exists" flag
.scan
    ld a, c
    call PcbPtr                 ; DE=&PCB[c] (preserves BC)
    ld a, [de]
    cp PS_FREE
    jr z, .next
    ; parent == my pid ?
    ld h, d
    ld l, e
    ld a, l
    add PROC_PARENT
    ld l, a
    ld a, h
    adc 0
    ld h, a
    ld a, [wWaitMyPid]
    cp [hl]
    jr nz, .next
    ld b, 1                     ; we have at least one child
    ld a, [de]                  ; state (DE still = base)
    cp PS_ZOMBIE
    jr z, .reap
.next
    inc c
    ld a, c
    cp MAX_PROCS
    jr c, .scan
    ; finished scan: any children?
    ld a, b
    or a
    jr z, .nochild
    ; children exist but none are zombies: block and yield, then retry
    ld a, [wCurProc]
    ld l, a
    ld a, [wCurProc+1]
    ld h, a
    ld a, PS_BLOCKED
    ld [hl], a
    call SchedYield
    ld a, [wCurProc]
    ld l, a
    ld a, [wCurProc+1]
    ld h, a
    ld a, PS_READY
    ld [hl], a
    jr .retry
.reap
    ; DE = &zombie child PCB
    ld h, d
    ld l, e
    inc hl                      ; ->PROC_PID
    ld a, [hl]
    ld [wWaitRetPid], a
    ld a, e
    add PROC_EXIT
    ld l, a
    ld a, d
    adc 0
    ld h, a
    ld a, [hl]
    ld [wWaitRetCode], a
    ld a, e
    add PROC_RAMB
    ld l, a
    ld a, d
    adc 0
    ld h, a
    ld a, [hl]
    call FreeRamBank            ; recycle the child's cart-RAM bank
    ld a, PS_FREE
    ld [de], a                  ; free the PCB slot
    ld a, [wWaitRetCode]
    ld b, a
    ld a, [wWaitRetPid]
    ret
.nochild
    ld a, $FF
    ret

; =============================================================================
; sys_kill(B = pid) -> A = 0 ok / $FF fail. pid 1 (init/shell) is immortal.
; Terminates the target: mark zombie, reparent its children to init, wake its
; parent if blocked in wait. If we killed ourselves, schedule away forever.
; =============================================================================
sys_kill::
    ld a, b
    cp INIT_PID
    jr z, .fail                 ; init is immortal
    call FindPcbByPid           ; A=pid -> DE=&PCB, CF if not found (B preserved=pid)
    jr c, .fail
    ld a, [de]                  ; state
    cp PS_FREE
    jr z, .fail
    cp PS_ZOMBIE
    jr z, .fail                 ; already dead
    ; mark ZOMBIE
    ld a, PS_ZOMBIE
    ld [de], a
    ; PROC_EXIT = KILL_CODE
    ld a, e
    add PROC_EXIT
    ld l, a
    ld a, d
    adc 0
    ld h, a
    ld a, KILL_CODE
    ld [hl], a
    ; victim's parent pid
    ld a, e
    add PROC_PARENT
    ld l, a
    ld a, d
    adc 0
    ld h, a
    ld a, [hl]
    ld [wKillParent], a
    ; reparent victim's children to init (B still = victim pid)
    ld a, b
    ld [wExitMyPid], a
    call ReparentToInit
    ; wake victim's parent if it is blocked in wait()
    ld a, [wKillParent]
    call FindPcbByPid
    jr c, .maybe_self
    ld a, [de]
    cp PS_BLOCKED
    jr nz, .maybe_self
    ld a, PS_READY
    ld [de], a
.maybe_self
    ; if the victim is the current process, don't return to it
    ld a, [wCurProc]
    add PROC_PID
    ld l, a
    ld a, [wCurProc+1]
    adc 0
    ld h, a
    ld a, [wExitMyPid]
    cp [hl]
    jr nz, .ok
.dead
    call SchedYield
    jr .dead
.ok
    xor a
    ret
.fail
    ld a, $FF
    ret

; =============================================================================
; sys_ps(B = slot, DE = buf[3]) -> A = 1 if used (buf = pid,state,prog), else 0
; =============================================================================
sys_ps::
    ld a, e
    ld [wPsBuf], a
    ld a, d
    ld [wPsBuf+1], a
    ld a, b
    cp MAX_PROCS
    jr nc, .no
    call PcbPtr                 ; DE = &PCB[slot]
    ld a, [de]                  ; state
    cp PS_FREE
    jr z, .no
    ld a, [wPsBuf]
    ld l, a
    ld a, [wPsBuf+1]
    ld h, a                     ; HL = buf
    push de                     ; PCB
    ld a, e
    add PROC_PID
    ld e, a
    ld a, d
    adc 0
    ld d, a
    ld a, [de]                  ; pid
    ld [hl+], a
    pop de
    push de
    ld a, [de]                  ; state
    ld [hl+], a
    pop de
    ld a, e
    add PROC_PROG
    ld e, a
    ld a, d
    adc 0
    ld d, a
    ld a, [de]                  ; prog id
    ld [hl], a
    ld a, 1
    ret
.no
    xor a
    ret

; =============================================================================
; sys_progname(B = id, DE = namebuf) - copy the command name for a program id.
; =============================================================================
sys_progname::
    ld hl, NameTable
.l
    ld a, [hl]
    or a
    jr z, .none
    ld a, l
    ld [wPnStart], a
    ld a, h
    ld [wPnStart+1], a
.sk
    ld a, [hl+]
    or a
    jr nz, .sk                  ; HL now at the id byte
    ld a, [hl]
    cp b
    jr z, .match
    inc hl                      ; skip id -> next entry
    jr .l
.match
    ld a, [wPnStart]
    ld l, a
    ld a, [wPnStart+1]
    ld h, a
    ld b, 8
.cp
    ld a, [hl+]
    ld [de], a
    inc de
    or a
    jr z, .done
    dec b
    jr nz, .cp
.done
    ret
.none
    ld a, $3F                   ; '?'
    ld [de], a
    inc de
    xor a
    ld [de], a
    ret

; =============================================================================
; sys_reap() -> A = pid of a reaped zombie child, or 0 (never blocks).
; Used by the shell to clean up finished background jobs.
; =============================================================================
sys_reap::
    ld a, [wCurProc]
    add PROC_PID
    ld l, a
    ld a, [wCurProc+1]
    adc 0
    ld h, a
    ld a, [hl]
    ld [wWaitMyPid], a
    ld c, 0
.scan
    ld a, c
    call PcbPtr                 ; DE = &PCB[c]
    ld a, [de]
    cp PS_ZOMBIE
    jr nz, .next
    ld h, d
    ld l, e
    ld a, l
    add PROC_PARENT
    ld l, a
    ld a, h
    adc 0
    ld h, a
    ld a, [wWaitMyPid]
    cp [hl]
    jr nz, .next
    ; reap: pid, free bank, free slot
    ld h, d
    ld l, e
    inc hl                      ; PROC_PID
    ld a, [hl]
    ld [wWaitRetPid], a
    ld a, e
    add PROC_RAMB
    ld l, a
    ld a, d
    adc 0
    ld h, a
    ld a, [hl]
    call FreeRamBank
    ld a, PS_FREE
    ld [de], a
    ld a, [wWaitRetPid]
    ret
.next
    inc c
    ld a, c
    cp MAX_PROCS
    jr c, .scan
    xor a
    ret