Welcome to mirror list, hosted at ThFree Co, Russian Federation.

crt0.S « or1k « libgloss - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f1753d0d7b097d2e1a9cf6f62ab56a54f4745c97 (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
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
/* crt0.S -- startup file for OpenRISC 1000.
 *
 * Copyright (c) 2011, 2014 Authors
 *
 * Contributor Julius Baxter <juliusbaxter@gmail.com>
 * Contributor Stefan Wallentowitz <stefan.wallentowitz@tum.de>
 *
 * The authors hereby grant permission to use, copy, modify, distribute,
 * and license this software and its documentation for any purpose, provided
 * that existing copyright notices are retained in all copies and that this
 * notice is included verbatim in any distributions. No written agreement,
 * license, or royalty fee is required for any of the authorized uses.
 * Modifications to this software may be copyrighted by their authors
 * and need not follow the licensing terms described here, provided that
 * the new terms are clearly indicated on the first page of each file where
 * they apply.
 */

/* -------------------------------------------------------------------------- */
/* Coding convention:
   Assembly is hard to read per se, so please follow the following coding
   conventions to keep it consistent and ease reading:
    * internal jump labels start with L, no identation
    * assemble lines have one tab identation
    * attributes (.section, .global, ..) are indented with one tab
    * code is structured using tabs, i.e., use 'l.sw\t0(r1),r1' with a single
      tab. libgloss assumes 8 space tab width, so that might look unstructured
      with tab widths below 6. Nevertheless don't use spaces or two tabs.
    * no space after comma
    * use the defined macros if possible as they reduce errors
    * use OR1K_INST with OR1K_DELAYED(_NOP)
    * OR1K_DELAYED is multiline for better readability, the inner parts are
      indented with another tab.
    * COMMENT! Try to accompy every line with a meaningful comment. If possible
      use pseudo code to describe the code. Also mention intentions and not only
      the obvious things..                                                    */
/* -------------------------------------------------------------------------- */

#include "include/or1k-asm.h"
#include "include/or1k-sprs.h"

/* -------------------------------------------------------------------------- */
// Stack definitions
/* -------------------------------------------------------------------------- */

// Stacks
// Memory layout:
//  +--------------------+ <- board_mem_base+board_mem_size/exception_stack_top
//  | exception stack(s) |
//  +--------------------+ <- stack_top
//  |     stack(s)       |
//  +--------------------+ <- stack_bottom
//  |      heap          |
//  +--------------------+
//  | text, data, bss..  |
//  +--------------------+

// Reserved stack size
#define STACK_SIZE 8192

// Reserved stack size for exceptions (can usually be smaller than normal stack)
#define EXCEPTION_STACK_SIZE 8192

// Size of redzone + size of space required to store state
// This value must match that in the support library or1k_exception_handler
// function
#define EXCEPTION_STACK_FRAME (128+128)

	.extern _or1k_stack_top    /* points to the next address after the stack */
	.extern _or1k_stack_bottom /* points to the last address in the stack */
	.extern _or1k_exception_stack_top
	.extern _or1k_exception_stack_bottom

	.section .data
	.global _or1k_stack_size /* reserved stack size */
	.global _or1k_exception_stack_size

_or1k_stack_size:		.word STACK_SIZE
_or1k_exception_stack_size:	.word EXCEPTION_STACK_SIZE

#ifdef __OR1K_MULTICORE__
	.extern _or1k_stack_core
	.extern _or1k_exception_stack_core
#endif

#define SHADOW_REG(x) (OR1K_SPR_SYS_GPR_BASE + 32 + x)

/* -------------------------------------------------------------------------- */
/*!Macro to handle exceptions.

  Load NPC into r3, EPCR into r4
                                                                              */
/* -------------------------------------------------------------------------- */

#define GPR_BUF_OFFSET(x) (x << 2)

#ifndef __OR1K_MULTICORE__
#define CALL_EXCEPTION_HANDLER					\
	/* store current stack pointer to address 4 */		\
	l.sw	0x4(r0),r1;					\
	/* Load top of the exception stack */			\
	l.movhi	r1,hi(_or1k_exception_stack_top);		\
	l.ori	r1,r1,lo(_or1k_exception_stack_top);		\
	l.lwz	r1,0(r1);					\
	/* Reserve red zone and context space */		\
	l.addi	r1,r1,-EXCEPTION_STACK_FRAME;			\
	/* Store GPR3 in context */				\
	l.sw	GPR_BUF_OFFSET(3)(r1),r3;			\
	/* Load back software's stack pointer */		\
	l.lwz	r3,0x4(r0);					\
	/* Store this in the context */				\
	l.sw	GPR_BUF_OFFSET(1)(r1),r3;			\
	/* Store GPR4 in the context */				\
	l.sw	GPR_BUF_OFFSET(4)(r1),r4;			\
	/* Copy the current program counter as first */		\
	/* argument for the exception handler. This */		\
	/* is then used to determine the exception. */		\
	l.mfspr	r3,r0,OR1K_SPR_SYS_NPC_ADDR;			\
	OR1K_DELAYED(						\
	/* Copy program counter of exception as */		\
	/* second argument to the exception handler */		\
		OR1K_INST(l.mfspr r4,r0,OR1K_SPR_SYS_EPCR_BASE),\
	/* Jump to exception handler. This will rfe */		\
		OR1K_INST(l.j _or1k_exception_handler)		\
	)
#else
#define CALL_EXCEPTION_HANDLER					\
	/* store current stack pointer to shadow reg */		\
	l.mtspr	r0,r1,SHADOW_REG(1);				\
	/* store current GPR3 as we need it */			\
	l.mtspr	r0,r3,SHADOW_REG(2);				\
	/* Load pointer to exception stack array */		\
	l.movhi	r1,hi(_or1k_exception_stack_core);		\
	l.ori	r1,r1,lo(_or1k_exception_stack_core);		\
	l.lwz	r1,0(r1);					\
	/* Get core id */					\
	l.mfspr	r3,r0,OR1K_SPR_SYS_COREID_ADDR;			\
	/* Calculate offset in array */				\
	l.slli	r3,r3,2;					\
	/* Load value from array to stack pointer */		\
	l.add	r1,r1,r3;					\
	l.lwz	r1,0(r1);					\
	/* Reserve red zone and context space */		\
	l.addi	r1,r1,-EXCEPTION_STACK_FRAME;			\
	/* Load back software's stack pointer */		\
	l.mfspr	r3,r0,SHADOW_REG(1);				\
	/* Store this in the context */				\
	l.sw	GPR_BUF_OFFSET(1)(r1),r3;			\
	/* Load back GPR3 */					\
	l.mfspr	r3,r0,SHADOW_REG(2);				\
	/* Store this in the context */				\
	l.sw	GPR_BUF_OFFSET(3)(r1),r3;			\
	/* Store GPR4 in the context */				\
	l.sw	GPR_BUF_OFFSET(4)(r1),r4;			\
	/* Copy the current program counter as first */		\
	/* argument for the exception handler. This */		\
	/* is then used to determine the exception. */		\
	l.mfspr	r3,r0,OR1K_SPR_SYS_NPC_ADDR;			\
	OR1K_DELAYED(						\
	/* Copy program counter of exception as */		\
	/* second argument to the exception handler */		\
		OR1K_INST(l.mfspr r4,r0,OR1K_SPR_SYS_EPCR_BASE),\
	/* Jump to exception handler. This will rfe */		\
		OR1K_INST(l.j _or1k_exception_handler)		\
	)
#endif

/* -------------------------------------------------------------------------- */
/*!Exception vectors                                                          */
/* -------------------------------------------------------------------------- */
	.section .vectors,"ax"

	/* 0x100: RESET exception */
	.org 0x100
_or1k_reset:
	l.movhi	r0,0
#ifdef __OR1K_MULTICORE__
	// This is a hack that relies on the fact, that all cores start at the
	// same time and they are similarily fast
	l.sw	0x4(r0),r0
	// Similarly, we use address 8 to signal how many cores have exit'ed
	l.sw	0x8(r0),r0
#endif
	l.movhi	r1,0
	l.movhi	r2,0
	l.movhi	r3,0
	l.movhi	r4,0
	l.movhi	r5,0
	l.movhi	r6,0
	l.movhi	r7,0
	l.movhi	r8,0
	l.movhi	r9,0
	l.movhi	r10,0
	l.movhi	r11,0
	l.movhi	r12,0
	l.movhi	r13,0
	l.movhi	r14,0
	l.movhi	r15,0
	l.movhi	r16,0
	l.movhi	r17,0
	l.movhi	r18,0
	l.movhi	r19,0
	l.movhi	r20,0
	l.movhi	r21,0
	l.movhi	r22,0
	l.movhi	r23,0
	l.movhi	r24,0
	l.movhi	r25,0
	l.movhi	r26,0
	l.movhi	r27,0
	l.movhi	r28,0
	l.movhi	r29,0
	l.movhi	r30,0
	l.movhi	r31,0

	/* Clear status register, set supervisor mode */
	l.ori	r1,r0,OR1K_SPR_SYS_SR_SM_MASK
	l.mtspr	r0,r1,OR1K_SPR_SYS_SR_ADDR
	/* Clear timer mode register*/
	l.mtspr	r0,r0,OR1K_SPR_TICK_TTMR_ADDR
	/* Jump to program initialisation code */
	LOAD_SYMBOL_2_GPR(r4, _or1k_start)
	OR1K_DELAYED_NOP(OR1K_INST(l.jr r4))

	.org 0x200
	CALL_EXCEPTION_HANDLER

	/* 0x300: Data Page Fault exception */
	.org 0x300
	CALL_EXCEPTION_HANDLER

	/* 0x400: Insn Page Fault exception */
	.org 0x400
	CALL_EXCEPTION_HANDLER

	/* 0x500: Timer exception */
	.org 0x500
	CALL_EXCEPTION_HANDLER

	/* 0x600: Aligment exception */
	.org 0x600
	CALL_EXCEPTION_HANDLER

	/* 0x700: Illegal insn exception */
	.org 0x700
	CALL_EXCEPTION_HANDLER

	/* 0x800: External interrupt exception */
	.org 0x800
	CALL_EXCEPTION_HANDLER

	/* 0x900: DTLB miss exception */
	.org 0x900
	CALL_EXCEPTION_HANDLER

	/* 0xa00: ITLB miss exception */
	.org 0xa00
	CALL_EXCEPTION_HANDLER

	/* 0xb00: Range exception */
	.org 0xb00
	CALL_EXCEPTION_HANDLER

	/* 0xc00: Syscall exception */
	.org 0xc00
	CALL_EXCEPTION_HANDLER

	/* 0xd00: Floating point exception */
	.org 0xd00
	CALL_EXCEPTION_HANDLER

	/* 0xe00: Trap exception */
	.org 0xe00
	CALL_EXCEPTION_HANDLER

	/* 0xf00: Reserved exceptions */
	.org 0xf00
	CALL_EXCEPTION_HANDLER

	.org 0x1000
	CALL_EXCEPTION_HANDLER

	.org 0x1100
	CALL_EXCEPTION_HANDLER

	.org 0x1200
	CALL_EXCEPTION_HANDLER

	.org 0x1300
	CALL_EXCEPTION_HANDLER

	.org 0x1400
	CALL_EXCEPTION_HANDLER

	.org 0x1500
	CALL_EXCEPTION_HANDLER

	.org 0x1600
	CALL_EXCEPTION_HANDLER

	.org 0x1700
	CALL_EXCEPTION_HANDLER

	.org 0x1800
	CALL_EXCEPTION_HANDLER

	.org 0x1900
	CALL_EXCEPTION_HANDLER

	.org 0x1a00
	CALL_EXCEPTION_HANDLER

	.org 0x1b00
	CALL_EXCEPTION_HANDLER

	.org 0x1c00
	CALL_EXCEPTION_HANDLER

	.org 0x1d00
	CALL_EXCEPTION_HANDLER

	.org 0x1e00
	CALL_EXCEPTION_HANDLER

	.org 0x1f00
	CALL_EXCEPTION_HANDLER

	/* Pad to the end */
	.org 0x1ffc
	l.nop

/* -------------------------------------------------------------------------- */
/*!Main entry point

  This is the initialization code of the library. It performs these steps:

   * Call early board initialization:
     Before anything happened, the board support may do some very early
     initialization. This is at maximum some very basic stuff that would
     otherwise prevent the following code from functioning. Other initialization
     of peripherals etc. is done later (before calling main).
     See the description below and README.board for details.

   * Initialize the stacks:
     Two stacks are configured: The system stack is used by the software and
     the exception stack is used when an exception occurs. We added this as
     this should be flexible with respect to the usage of virtual memory.

   * Activate the caches:
     If available the caches are initiliazed and activated.

   * Clear BSS:
     The BSS are essentially the uninitialized C variables. They are set to 0
     by default. This is performed by this function.

   * Initialize the impure data structure:
     Similarly, we need two library contexts, one for the normal software and
     one that is used during exceptions. The impure data structure holds
     the context information of the library. The called C function will setup
     both data structures. There is furthermore a pointer to the currently
     active impure data structure, which is initially set to the normal one.

   * Initialize or1k support library reentrant data structures

   * Initialize constructors:
     Call the static and global constructors

   * Set up destructors to call from exit
     The library will call the function set via atexit() during exit(). We set
     it to call the _fini function which performs destruction.

   * Call board initialization:
     The board initialization can perform board specific initializations such as
     configuring peripherals etc.

   * Jump to main
     Call main with argc = 0 and *argv[] = 0

   * Call exit after main returns
     Now we call exit()

   * Loop forever
     We are dead.
*/

/* -------------------------------------------------------------------------- */
	.section	.text

	/* Following externs from board-specific object passed at link time */
	.extern _or1k_board_mem_base
	.extern _or1k_board_mem_size
	.extern _or1k_board_uart_base

	/* The early board initialization may for example read the memory size and
	   set the mem_base and mem_size or do some preliminary board
	   initialization. As we do not have a stack at this time, the function may
	   not use the stack (and therefore be a or call a C function. But it can
	   safely use all registers.

	   We define a default implementation, which allows board files in C. As
	   described above, this can only be used in assembly (board_*.S) as at
	   the early stage not stack is available. A board that needs early
	   initialization can overwrite the function with .global _board_init_early.

	   Recommendation: Only use when you really need it! */
	.weak _or1k_board_init_early
_or1k_board_init_early:
	OR1K_DELAYED_NOP(OR1K_INST(l.jr r9))

	/* The board initialization is then called after the C library and UART
	   are initialized. It can then be used to configure UART or other
	   devices before the actual main function is called. */
	.extern _or1k_board_init

	.global	_or1k_start
	.type	_or1k_start,@function
_or1k_start:
	/* It is good to initialize and enable the caches before we do anything,
	   otherwise the cores will continuously access the bus during the wait
	   time for the boot barrier (0x4).
	   Fortunately or1k_cache_init does not need a stack */
	OR1K_DELAYED_NOP(OR1K_INST(l.jal _or1k_cache_init))

#ifdef __OR1K_MULTICORE__
	// All but core 0 have to wait
	l.mfspr	r1, r0, OR1K_SPR_SYS_COREID_ADDR
	l.sfeq	r1, r0
	OR1K_DELAYED_NOP(OR1K_INST(l.bf .Lcore0))
.Lspin:
	/* r1 will be used by the other cores to check for the boot variable
	   Check if r1 is still zero, core 0 will set it to 1 once it booted
	   As the cache is already turned on, this will not create traffic on
	   the bus, but the change is snooped by cache coherency then */
	l.lwz r1,0x4(r0)
	l.sfeq r1, r0
	OR1K_DELAYED_NOP(OR1K_INST(l.bf .Lspin))

	/* Initialize core i stack */
	// _or1k_stack_core is the array of stack pointers
	LOAD_SYMBOL_2_GPR(r2,_or1k_stack_core)
	// Load the base address
	l.lwz	r2,0(r2)
	// Generate offset in array
	l.mfspr	r1,r0,OR1K_SPR_SYS_COREID_ADDR
	l.slli	r1,r1,2
	// Add to array base
	l.add	r2,r2,r1
	// Load pointer to the stack top and set frame pointer
	l.lwz	r1,0(r2)
	l.or	r2,r1,r1

	// The slave cores are done, jump to main part
	OR1K_DELAYED_NOP(OR1K_INST(l.j .Linit_done));

	/* Only core 0 executes the initialization code */
.Lcore0:
#endif
	/* Call early board initialization */
	OR1K_DELAYED_NOP(OR1K_INST(l.jal _or1k_board_init_early))

	/* Clear BSS */
.Lclear_bss:
	LOAD_SYMBOL_2_GPR(r3,__bss_start)
	LOAD_SYMBOL_2_GPR(r4,end)

.Lclear_bss_loop:
	l.sw	(0)(r3),r0
	l.sfltu r3,r4
	OR1K_DELAYED(
		OR1K_INST(l.addi r3,r3,4),
		OR1K_INST(l.bf .Lclear_bss_loop)
	)

	/* Initialise stack and frame pointer (set to same value) */
	LOAD_SYMBOL_2_GPR(r1,_or1k_board_mem_base)
	l.lwz	r1,0(r1)
	LOAD_SYMBOL_2_GPR(r2,_or1k_board_mem_size)
	l.lwz	r2,0(r2)
	l.add	r1,r1,r2

	/* Store exception stack top address */
	LOAD_SYMBOL_2_GPR(r3,_or1k_exception_stack_top)
	l.sw	0(r3),r1

	/* Store exception stack bottom address */
	// calculate bottom address
	// r3 = *exception stack size
	LOAD_SYMBOL_2_GPR(r3,_or1k_exception_stack_size)
	// r3 = exception stack size
	l.lwz	r3,0(r3)
#ifdef __OR1K_MULTICORE__
	l.mfspr	r4,r0,OR1K_SPR_SYS_NUMCORES_ADDR
	l.mul	r3,r4,r3
#endif
	// r4 = exception stack top - exception stack size = exception stack bottom
	l.sub	r4,r1,r3
	// r5 = *exception stack bottom
	LOAD_SYMBOL_2_GPR(r5,_or1k_exception_stack_bottom)
	// store
	l.sw	0(r5),r4

	// Move stack pointer accordingly
	l.or	r1,r0,r4
	l.or	r2,r1,r1

	/* Store stack top address */
	LOAD_SYMBOL_2_GPR(r3,_or1k_stack_top)
	l.sw	0(r3),r1

	/* Store stack bottom address */
	// calculate bottom address
	// r3 = stack size
	LOAD_SYMBOL_2_GPR(r3,_or1k_stack_size)
	l.lwz	r3,0(r3)
#ifdef __OR1K_MULTICORE__
	l.mfspr	r4, r0, OR1K_SPR_SYS_NUMCORES_ADDR
	l.mul	r3, r4, r3
#endif
	// r4 = stack top - stack size = stack bottom
	// -> stack bottom
	l.sub	r4,r1,r3
	// r5 = *exception stack bottom
	LOAD_SYMBOL_2_GPR(r5,_or1k_stack_bottom)
	// store to variable
	l.sw	0(r5),r4

	/* Reinitialize the or1k support library */
	OR1K_DELAYED_NOP(OR1K_INST(l.jal _or1k_init))

	/* Reinitialize the reentrancy structure */
	OR1K_DELAYED_NOP(OR1K_INST(l.jal _or1k_libc_impure_init))

	/* Call global and static constructors */
	OR1K_DELAYED_NOP(OR1K_INST(l.jal _init))

	/* Set up destructors to be called from exit if main ever returns */
	l.movhi	r3,hi(_fini)
	OR1K_DELAYED(
		OR1K_INST(l.ori r3,r3,lo(_fini)),
		OR1K_INST(l.jal atexit)
	)

	/* Check if UART is to be initialised */
	LOAD_SYMBOL_2_GPR(r4,_or1k_board_uart_base)
	l.lwz	r4,0(r4)
	/* Is base set? If not, no UART */
	l.sfne	r4,r0
	l.bnf	.Lskip_uart
	l.or	r3,r0,r0
	OR1K_DELAYED_NOP(OR1K_INST(l.jal _or1k_uart_init))

.Lskip_uart:
	/* Board initialization */
	OR1K_DELAYED_NOP(OR1K_INST(l.jal _or1k_board_init))

#ifdef __OR1K_MULTICORE__
	// Start other cores
	l.ori	r3, r0, 1
	l.sw	0x4(r0), r3
#endif

.Linit_done:
	/* Jump to main program entry point (argc = argv = envp = 0) */
	l.or	r3,r0,r0
	l.or	r4,r0,r0
	OR1K_DELAYED(
		OR1K_INST(l.or r5,r0,r0),
		OR1K_INST(l.jal main)
	)

#ifdef __OR1K_MULTICORE__
.incrementexit:
	/* Atomically increment number of finished cores */
	l.lwa	r3,0x8(r0)
	l.addi	r3,r3,1
	l.swa	0x8(r0),r3
	OR1K_DELAYED_NOP(OR1K_INST(l.bnf .incrementexit));
	/* Compare to number of cores in this cluster */
	l.mfspr	r4,r0, OR1K_SPR_SYS_NUMCORES_ADDR
	/* Compare to number of finished tasks */
	l.sfeq	r3,r4
	/* Last core needs to desctruct library etc. */
	OR1K_DELAYED_NOP(OR1K_INST(l.bf .exitcorelast));
	OR1K_DELAYED(
		OR1K_INST(l.addi r3,r11,0),
		OR1K_INST(l.jal _exit)
	)
.exitcorelast:
#endif
	/* If program exits, call exit routine */
	OR1K_DELAYED(
		OR1K_INST(l.addi r3,r11,0),
		OR1K_INST(l.jal exit)
	)

	/* Loop forever */
.Lloop_forever:
	OR1K_DELAYED_NOP(OR1K_INST(l.j .Lloop_forever))

	.size _or1k_start,.-_or1k_start