forked from th-otto/gulam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cstartv.s
494 lines (434 loc) · 12 KB
/
cstartv.s
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
****************
* extended cstart routine:
* - setting up argc and argv
* - setting up environment
* - shrink memory
* - and start main
*
* parameters are NOT wildcard expanded
* ARGV environment parameter is searched for instead
*
GLOBL exit
GLOBL __exit
GLOBL _BasPag
GLOBL _base
GLOBL __base
GLOBL _app
GLOBL errno
GLOBL _AtExitVec,_FilSysVec
GLOBL _stksize
GLOBL _StkLim
GLOBL _PgmSize
GLOBL environ
GLOBL __environ
GLOBL _init_environ
GLOBL __env_argv
GLOBL _argc
GLOBL _argv
GLOBL __text
GLOBL __data
GLOBL __bss
XREF main
XREF _fpuinit
XREF _StkSize
XREF _FreeAll
XREF _StdErrF
* Base page structure
OFFSET 0
TpaStart: ds.l 1
TpaEnd: ds.l 1
TextSegStart: ds.l 1
TextSegSize: ds.l 1
DataSegStart: ds.l 1
DataSegSize: ds.l 1
BssSegStart: ds.l 1
BssSegSize: ds.l 1
DtaPtr: ds.l 1
PntPrcPtr: ds.l 1
Reserved0: ds.l 1
EnvStrPtr: ds.l 1
Reserved1: ds.b 7
CurDrv: ds.b 1
Reserved2: ds.l 18
CmdLine: ds.b 128
BasePageSize: ds 0
DEBUG equ 0
PSTACKSIZE equ 0
XREF print_env
MACRO DBG_OUT string
IFNE DEBUG
LOCAL txt
data
txt: dc.b string
dc.b 13,10,0
text
movem.l d0-a6,-(a7)
move.l #txt,-(a7)
move.w #Cconws,-(a7)
trap #1
lea 6(a7),a7
movem.l (a7)+,d0-a6
ENDC
ENDM
TEXT
Cconws equ $09
Fsetdta equ $1a
Fgetdta equ $2f
Fseek equ $42
Fforce equ $46
Mshrink equ $4a
Pterm equ $4c
Fsfirst equ $4e
Fsnext equ $4f
__text:
Start: bra Start0
dc.l 0
_stksize: dc.l _StkSize * Stack size entry
EmpStr: dc.b 0,0
IFNE PSTACKSIZE
dc.b 'PROF'
ENDC
Start0: move.l a0,a3
move.l a3,d0
bne acc
move.l 4(a7),a3
moveq.l #1,d0
bra app
acc: moveq.l #0,d0
app: move.l a3,_BasPag
move.w d0,_app
movea.l TextSegSize(a3),a0
adda.l DataSegSize(a3),a0
adda.l BssSegSize(a3),a0
adda.w #BasePageSize,a0
move.l a0,_PgmSize
move.l a3,d0
add.l a0,d0
and.b #$FC,d0
movea.l d0,a7
* check application flag
* TST.W _app
* BEQ Start8 * No environment and no arguments
sub.l _stksize,d0
add.l #4,d0
and.b #$FC,d0
movea.l d0,a1
movea.l a1,a4
move.l a1,environ
move.l a1,_init_environ
ifne DEBUG
movem.l d0-a6,-(a7)
move.l a3,a0
jsr print_env
movem.l (a7)+,d0-a6
endc
movea.l EnvStrPtr(a3),a2
move.l a2,(a1)+
tst.l a2
beq noenv
Start1: tst.b (a2)+
bne Start1
move.l a2,(a1)+
tst.b (a2)+
bne Start1
clr.l -4(a1)
noenv:
movea.l a1,a2
move.l #EmpStr,(a1)+ ; argv[0] = ""
lea CmdLine(a3),a0
move.b (a0)+,d1
ext.w d1
cmp.w #127,d1
beq do_argv
bsr scan_cmd
do_argv:
move.l a4,-(a7) ; push envp
move.l a2,-(a7) ; push argv
move.w d3,-(a7) ; push argc
move.w d1,-(a7)
move.l a1,_StkLim
move.l _PgmSize,-(a7)
move.l a3,-(a7)
move.w #0,-(a7)
move.w #Mshrink,-(a7)
trap #1
lea 12(a7),a7
jsr _fpuinit
move.w (a7)+,d1
cmp.w #127,d1
* bne no_argv
DBG_OUT "argv indicator found"
lea 0(a7),a0
lea 2(a7),a1
move.l environ,a2
bsr argv
tst.b d2
bne no_argv
DBG_OUT "no ARGV found, using command line"
moveq #126,d1
lea CmdLine+1(a3),a0
move.w (a7)+,d3
move.l (a7)+,a2
move.l a2,a1
move.l #EmpStr,(a1)+
bsr scan_cmd
move.l a2,-(a7) ; push argv
move.w d3,-(a7) ; push argc
no_argv:
move.l #_StdErrF,a0
move.w #2,16(a0) ; fileno(stderr) = 2
tst.b d1
bne nostderr ; environ STDERR found -> shell should handle it
DBG_OUT "no STDERR env, checking redirection"
move.w #1,-(a7) ; offset = Fseek(0l, 2, SEEK_CUR)
move.w #2,-(a7)
clr.l -(a7)
move.w #Fseek,-(a7)
trap #1
move.l d0,d3
move.w #0,-(a7) ; Fseek(1l, 2, SEEK_SET)
move.w #2,-(a7)
move.l #1,-(a7)
move.w #Fseek,-(a7)
trap #1
exg d0,d3
move.w #0,-(a7) ; Fseek(offset, 2, SEEK_SET)
move.w #2,-(a7)
move.l d0,-(a7)
move.w #Fseek,-(a7)
trap #1
lea 30(a7),a7
subq.l #1,d3
beq nostderr ; not a tty -> don't change
DBG_OUT "redirecting handle 2"
move.w #-1,-(a7)
move.w #2,-(a7)
move.w #Fforce,-(a7)
trap #1
addq.l #6,a7
nostderr:
IFNE PSTACKSIZE
Setexc equ 5
move.l a7,a0
lea 8(a0),a0
move.l a0,_StkTop
move.l a0,_StkBot
pea catchtrap(pc)
move.w #32,-(a7) ; trap #0
move.w #Setexc,-(a7)
trap #13
addq.l #8,a7
move.l d0,old_trap
ENDC
move.w (a7),d0 ; restore argc
move.l 2(a7),a0 ; restore argv
movea.l a4,a1 ; restore envp
move.w d0,_argc
move.l a0,_argv
jsr main
exit: move.w d0,-(a7)
move.l _AtExitVec,d0
beq __exit
movea.l d0,a0
jsr (a0)
__exit:
IFNE PSTACKSIZE
XREF ltoa
move.l _StkTop,d0
move.l _StkBot,d1
sub.l d1,d0
moveq.l #10,d1
move.l #stackmsg1,a0
jsr ltoa
pea stackmsg
move.w #Cconws,-(a7)
trap #1
lea 6(a7),a7
pea stackmsg2
move.w #Cconws,-(a7)
trap #1
lea 6(a7),a7
move.l old_trap,-(a7)
move.w #32,-(a7)
move.w #Setexc,-(a7)
trap #13
addq.l #8,a7
ENDC
move.l _FilSysVec,d0
beq Exit1
movea.l d0,a0
jsr (a0)
Exit1: jsr _FreeAll
move.w #Pterm,-(a7)
trap #1
scan_cmd:
moveq #1,d3
clr.b 0(a0,d1)
Start2: move.b (a0)+,d1
beq Start99
cmpi.b #' '+1,d1
bmi Start2
addq.l #1,d3
subq.l #1,a0
cmpi.b #'"',d1
bne Start4
addq.l #1,a0
move.l a0,(a1)+
Start3: move.b (a0)+,d1
beq Start99
cmp.b #'"',d1
bne Start3
clr.b -1(a0)
bra Start2
Start4: move.l a0,(a1)+
Start6: move.b (a0)+,d1
beq Start7
cmp.b #' '+1,d1
bpl Start6
Start7: clr.b -1(a0)
bra Start2
Start99: clr.l (a1)+
rts
*
* passed: (a0) = &argc
* (a1) = &argv
* a2 = envp[]
*
* returns: d1 != 0 -> STDERR found
* returns: d2 != 0 -> ARGV found
*
argv: movem.l a3-a4,-(a7)
sf d1
sf d2
argv1: move.l (a2)+,a3
move.l a3,d0
beq xenv
cmp.b #'A',(a3)
beq argv2
cmp.b #'S',(a3)
bne argv1
cmp.b #'T',1(a3)
bne argv1
cmp.b #'D',2(a3)
bne argv1
cmp.b #'E',3(a3)
bne argv1
cmp.b #'R',4(a3)
bne argv1
cmp.b #'R',5(a3)
bne argv1
cmp.b #'=',6(a3)
bne argv1
st d1
bra argv1
argv2: cmp.b #'R',1(a3)
bne argv1
cmp.b #'G',2(a3)
bne argv1
cmp.b #'V',3(a3)
bne argv1
cmp.b #'A',(a3)
bne argv1
cmp.b #'=',4(a3)
bne argv1
st d2
clr.l -4(a2) ; terminate env[] array here
clr.b (a3) ; remove ARGV from env
lea 6(a3),a3
move.l a3,a4
move.l a3,__env_argv
scanslash: tst.b (a3)
beq endslash
cmp.b #$5c,(a3)+
bne scanslash
; move.l a3,a4
bra scanslash
endslash: move.l a4,a3
lowerit: move.b (a4)+,d0
beq lowerend
cmp.b #'A',d0
bcs lowerit
cmp.b #'Z'+1,d0
bcc lowerit
add.b #'a'-'A',d0
move.b d0,-1(a4)
bra lowerit
lowerend: move.l (a1),a4
clr.w (a0)
nxarg: move.l a3,(a4)+
add.w #1,(a0)
nxar1: tst.b (a3)+
bne nxar1
tst.b (a3)
bne nxarg
clr.l (a4)
xenv: movem.l (a7)+,a3-a4
rts
IFNE PSTACKSIZE
regoff equ 12
catchtrap: btst #5,(a7)
bne catch4
movem.l a0-a1/d1,-(a7)
move.l usp,a0
move.l _StkBot,d1
cmp.l a0,d1
bcs catch1
move.l a0,_StkBot
catch1: move.l regoff+2(a7),a1
move.w 0(a1),d1
cmp.w #$4e75,d1 ; rts
beq catch2
; simulate LINK opcode
addq.l #2,a1
move.l a1,regoff+2(a7)
move.l a6,-(a0)
move.l a0,a6
add.w d1,a0
bra catch3
catch2: ; simulate UNLK opcode
move.l a6,a0
move.l (a0)+,a6
catch3: move.l a0,usp
movem.l (a7)+,a0-a1/d1
rte
catch4: move.l a0,-(a7)
move.l 6(a7),a0
cmp.w #$4e75,(a0)
beq catch5
move.w #$4e56,-2(a0)
bra catch6
catch5: move.w #$4e5e,-2(a0)
catch6: subq.l #2,a0
move.l a0,6(a7)
move.l (a7)+,a0
pflusha
rte
ENDC
DATA
__data:
errno: dc.l 0
_AtExitVec: dc.l 0
_FilSysVec: dc.l 0
BSS
__bss:
_base:
__base:
_BasPag: ds.l 1
_app: ds.w 1
_StkLim: ds.l 1
_PgmSize: ds.l 1
__environ:
environ: ds.l 1
_init_environ: ds.l 1
__env_argv: ds.l 1
_argc: ds.w 1
_argv: ds.l 1
IFNE PSTACKSIZE
old_trap: ds.l 1
_StkTop: ds.l 1
_StkBot: ds.l 1
DATA
stackmsg: dc.b 'maximum stack size used: '
stackmsg1: dc.b ' ',0
stackmsg2: dc.b 13,10,0
ENDC