summaryrefslogtreecommitdiff
path: root/src/vectors.S
blob: 691d56863452b0d69c1447568c98c6ef61430dc2 (plain)
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
/* 
 *   <vectors.S>
 *      
 *   Second stage boot-loader and exception vectors for Open Hack'Ware.
 *   
 *   Copyright (C) 2004-2005 Jocelyn Mayer (l_indien@magic.fr)
 *   
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License V2
 *   as published by the Free Software Foundation
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#define ASSEMBLY_CODE
#include "bios.h"

.section .text
.align 2

.globl _start
_start:
        /* Entry point */
        li      r0, 0                                     ;
_turn_off_mmu:
        /* Be sure MMU is off and we are in 32 bits mode (for PPC64) */
        lis     r11, _hw_init@h                           ;
        ori     r11, r11, _hw_init@l                      ;
        mtspr   26, r11                                   ;
        mtspr   27, r0                                    ;
        rfi                                               ;
_hw_init:
        /* May need more hw init here */
_load_bios:
        /* Load the full BIOS into RAM */
        lis     r12, bios_base@h                          ;
        ori     r12, r12, bios_base@l                     ;
        lmw     r29, 0(r12)                               ;
        /* Set up the C stack */
        addis   r1, r29, 0x0040                           ;
        clrrwi  r1, r1, 19                                ;
        stw     r1, -16(r1)                               ;
        stwu    r0, -4(r1)                                ;
        stwu    r0, -4(r1)                                ;
        stwu    r0, -4(r1)                                ;
        stwu    r0, -4(r1)                                ;
        /* Skip frame pointer */        
        stwu    r0, -8(r1)                                ;
        stwu    r0, -4(r1)                                ;
        stwu    r0, -4(r1)                                ;
        /* Start copying */
        mtctr   r30                                       ;
        subi    r12, r3, 4                                ;
        subi    r13, r29, 4                               ;
_bios_copy_loop:
        lwzu    r14, 4(r12)                               ;
        stwu    r14, 4(r13)                               ;
        bdnz    _bios_copy_loop                           ;
        /* Synchronize the whole execution context */
        /* Also enable FPU */
        ori     r0, r0, (1 << 13)                         ;
        mtspr   26, r29                                   ;
        mtspr   27, r0                                    ;
        rfi                                               ;
        /* If we ever return, stop */
        bl      bug                                       ;

.org 0x0080
.section .text
.align 2
bug:
        /* Dump the exception and its context */
        mflr    r3                                        ;
        mfspr   r4, SRR0                                  ;
        mfspr   r5, SRR1                                  ;
        mfspr   r6, DAR                                   ;
        mfspr   r7, DSISR                                 ;
        /* Turn MMU off */
        lis     r0, _bug_no_mmu@h                         ;
        ori     r0, r0, _bug_no_mmu@l                     ;
        mtspr   26, r0                                    ;
        li      r0, 0                                     ;
        mtspr   27, r0                                    ;
        rfi                                               ;
_bug_no_mmu:
        bl      dump_exception                            ;
_forever:
        /* Loop forever */
        b       _forever                                  ;

skip_exception:
        /* Skip external interrupts and decrementer exception */
        /* BEWARE: be sure not to modify any register */
        stw     r11, save_area@l(0)                       ;
        mfspr   r11, 27                                   ;
        clrlwi  r11, r11, 16                              ;
        mtspr   27, r11                                   ;
        lwz     r11, save_area@l(0)                       ;
        rfi                                               ;

#define EXCP_BUG(entry)                                     \
.org 0x##entry                                            ; \
.section .text                                            ; \
.align 2                                                  ; \
excp_##entry:                                             ; \
        bl bug

#define EXCP_SKIP(entry)                                    \
.org 0x##entry                                            ; \
.section .text                                            ; \
.align 2                                                  ; \
excp_##entry##:                                           ; \
        b skip_exception

        /* Exception vectors */
        /* Reset exception */
        EXCP_BUG(0100)                                    ;

        /* Machine check exception */
        EXCP_BUG(0200)                                    ;

        /* DSI exception */
        EXCP_BUG(0300)                                    ;

        /* ISI exception */
        EXCP_BUG(0400)                                    ;

        /* External interrupt: skip it */
        EXCP_SKIP(0500)                                   ;

        /* Alignment exception */
        EXCP_BUG(0600)                                    ;

        /* Program exception */
        EXCP_BUG(0700)                                    ;

        /* No floating point exception */
        EXCP_BUG(0800)                                    ;

        /* Decrementer exception: skip it */
        EXCP_SKIP(0900)                                   ;

        /* Reserved A exception */
        EXCP_BUG(0A00)                                    ;

        /* Reserved B exception */
        EXCP_BUG(0B00)                                    ;

        /* System call exception */
        EXCP_BUG(0C00)                                    ;

        /* Trace exception */
        EXCP_BUG(0D00)                                    ;

        /* Floating point assist exception */
        EXCP_BUG(0E00)                                    ;

        /* Performance monitor exception */
        EXCP_BUG(0F00)                                    ;

        /* Instruction TLB miss exception */
        EXCP_BUG(1000)                                    ;

        /* Data TLB miss for store exception */
        EXCP_BUG(1100)                                    ;

        /* Data TLB miss for load exception */
        EXCP_BUG(1200)                                    ;

        /* Instruction address breakpoint exception */
        EXCP_BUG(1300)                                    ;

        /* System management interrupt exception */
        EXCP_BUG(1400)                                    ;

        /* Thermal management exception */
        EXCP_BUG(1500)                                    ;

        /* Unknown exceptions */
        EXCP_BUG(1600)                                    ;

        EXCP_BUG(1700)                                    ;

        EXCP_BUG(1800)                                    ;

        EXCP_BUG(1900)                                    ;

        EXCP_BUG(1A00)                                    ;

        EXCP_BUG(1B00)                                    ;

        EXCP_BUG(1C00)                                    ;

        EXCP_BUG(1D00)                                    ;

        EXCP_BUG(1E00)                                    ;

        EXCP_BUG(1F00)                                    ;
        /* End of exception vectors list */

.org 0x2000
.section .text
.align 2
helpers_start:

outb:
        /* void outb (uint32_t port, uint32_t data);
         * Writes a single character on an IO port.
         * Used for serial console.
         */
        stb     r4, 0(r3)                                 ;
        eieio                                             ;
        blr                                               ;

outstr:
        /* void outstr (uint32_t port, const unsigned char *str);
         * Writes a string on an IO port.
         */
        mflr    r20                                       ;
        subi    r11, r4, 1                                ;
        
_outstr_next:
        lbzu    r4, 1(r11)                                ;
        cmpi    0, r4, 0                                  ;
        beq     _outstr_done                              ;
        bl      outb                                      ;
        b       _outstr_next                              ;
_outstr_done:
        mtlr    r20                                       ;
        blr                                               ;

outdigit:
        /* void outdigit (uint32_t port, uint32_t digit);
         * Dumps a single digit on serial port.
         */
        mflr    r20                                       ;
        addi    r4, r4, '0'                               ;
        bl      outb                                      ;
        mtlr    r20                                       ;
        blr                                               ;

outhex:
        /* void outhex (uint32_t port, uint32_t value);
         * Dumps a 32 bits hex number on serial port
         */
        mflr    r21
        li      r11, 8                                    ;
        mtctr   r11                                       ;
        mr      r11, r4                                   ;
_outhex_next:
        rlwinm  r11, r11, 4, 0, 31                        ;
        clrlwi  r4, r11, 28                               ;
        cmpi    0, r4, 9                                  ;
        bgt     _outhex_xdigit                            ;
        bl      outdigit                                  ;
        bdnz    _outhex_next                              ;
        b       _outhex_done                              ;
_outhex_xdigit:
        addi    r4, r4, 'a' - 10                          ;
        bl      outb                                      ;
        bdnz    _outhex_next                              ;
_outhex_done:
        mtlr    r21                                       ;
        blr                                               ;

        /* void dump_exception (uint32_t lr, uint32_t srr0, uint32_t srr1,
         *                      uint32_t dar, uint32_t dsisr);
         * Dump a message when catching an exception
         */
dump_exception:
        /* Save call parameters */
        mflr    r19                                       ;
        mr      r22, r3                                   ;
        mr      r23, r4                                   ;
        mr      r24, r5                                   ;
        mr      r25, r6                                   ;
        mr      r26, r7                                   ;
        lis     r11, registers_area@h                     ;
        ori     r11, r11, registers_area@l                ;
        lmw     r27, 0(r11)                               ;
        /* Now, serial IO port is in r27,
         * message table start is in r28,
         * first exception message offset is in r29,
         * and last known exception number is in r30
         */
        /* Print error prompt message */
        mr      r3, r27                                   ;
        lwzu    r4, 4(r28)                                ;
        bl      outstr                                    ;
        /* Find message corresponding to the caught exception */
        srwi    r12, r22, 8                               ;
        cmp     0, r12, r30                               ;
        ble     _dump_excp_msg                            ;
        subi    r12, r30, 1                               ;
_dump_excp_msg:
        rlwinm  r12, r12, 2, 0, 31                        ;
        /* Dump execption message */
        mr      r3, r27                                   ;
        lwzx    r4, r12, r29                              ;
        bl      outstr                                    ;
        /* Complete exception message */
        mr      r3, r27                                   ;
        lwzu    r4, 4(r28)                                ;
        bl      outstr                                    ;
        /* Dump nip */
        mr      r3, r27                                   ;
        lwzu    r4, 4(r28)                                ;
        bl      outstr                                    ;
        mr      r3, r27                                   ;
        mr      r4, r23                                   ;
        bl      outhex                                    ;
        /* dump msr */
        mr      r3, r27                                   ;
        lwzu    r4, 4(r28)                                ;
        bl      outstr                                    ;
        mr      r3, r27                                   ;
        mr      r4, r24                                   ;
        bl      outhex                                    ;
        /* dump dar */
        mr      r3, r27                                   ;
        lwzu    r4, 4(r28)                                ;
        bl      outstr                                    ;
        mr      r3, r27                                   ;
        mr      r4, r25                                   ;
        bl      outhex                                    ;
        /* dump dsisr */
        mr      r3, r27                                   ;
        lwzu    r4, 4(r28)                                ;
        bl      outstr                                    ;
        mr      r3, r27                                   ;
        mr      r4, r26                                   ;
        bl      outhex                                    ;
        /* All done, dump last message and return */
        mr      r3, r27                                   ;
        lwzu    r4, 4(r28)                                ;
        bl      outstr                                    ;
        mtlr    r19                                       ;
        blr                                               ;

.section .rodata
.align 2
_BUG_message_0:
        .string "ERROR: BUG caught...\n"
_BUG_message_1:
        .string " exception"
_BUG_message_2:
        .string "\nnip=0x"
_BUG_message_3:
        .string " msr=0x"
_BUG_message_4:
        .string " dar=0x"
_BUG_message_5:
        .string " dsisr=0x"
_BUG_message_6:
        .string "\nStopping execution\n"

_excp_message_0x00:
        .string "BIOS execution"
_excp_message_0x01:
        .string "Reset"
_excp_message_0x02:
        .string "Machine check"
_excp_message_0x03:
        .string "Data memory access"
_excp_message_0x04:
        .string "Instruction fetch"
_excp_message_0x05:
        .string "External"
_excp_message_0x06:
        .string "Alignment"
_excp_message_0x07:
        .string "Program"
_excp_message_0x08:
        .string "No floating point"
_excp_message_0x09:
        .string "Decrementer"
_excp_message_0x0a:
        .string "Reserved A"
_excp_message_0x0b:
        .string "Reserved B"
_excp_message_0x0c:
        .string "System call"
_excp_message_0x0d:
        .string "Trace"
_excp_message_0x0e:
        .string "Floating point assist"
_excp_message_0x0f:
        .string "Performance monitor"
_excp_message_0x10:
        .string "Instruction TLB miss"
_excp_message_0x11:
        .string "Data TLB miss for store"
_excp_message_0x12:
        .string "Data TLB miss for load"
_excp_message_0x13:
        .string "Instruction address breakpoint"
_excp_message_0x14:
        .string "System management"
_excp_message_0x15:
        .string "Thermal management"
_excp_message_0x16:
        .string "Unknown"
_messages_table:
        .long _BUG_message_0
        .long _BUG_message_1
        .long _BUG_message_2
        .long _BUG_message_3
        .long _BUG_message_4
        .long _BUG_message_5
        .long _BUG_message_6
_excp_messages_table:
        .long _excp_message_0x00
        .long _excp_message_0x01
        .long _excp_message_0x02
        .long _excp_message_0x03
        .long _excp_message_0x04
        .long _excp_message_0x05
        .long _excp_message_0x06
        .long _excp_message_0x07
        .long _excp_message_0x08
        .long _excp_message_0x09
        .long _excp_message_0x0a
        .long _excp_message_0x0b
        .long _excp_message_0x0c
        .long _excp_message_0x0d
        .long _excp_message_0x0e
        .long _excp_message_0x0f
        .long _excp_message_0x10
        .long _excp_message_0x11
        .long _excp_message_0x12
        .long _excp_message_0x13
        .long _excp_message_0x14
        .long _excp_message_0x15
        .long _excp_message_0x16
_last_excp_message:

bios_base:
        .long BIOS_BASE
bios_size:
        .long BIOS_SIZE / 4
_dummy_0:
        .long 0x00000000

registers_area: /* To be loaded in register when an exception is caught */
_serial_IO:      /* r27 */
        .long 0x800003F8
_messages_start: /* r28 */
        .long _messages_table - 4
_excp_messages:  /* r29 */
        .long _excp_messages_table
_max_excp:       /* r30 */
        .long (_last_excp_message - _excp_messages_table) / 4
_dummy_1:        /* r31: dummy */
        .long 0x00000000
        
.section .data
.align 2
save_area: /* Area for r11 save when an exception is skipped */
        .long 0x00000000