OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [hal/] [common/] [v2_0/] [doc/] [hal.sgml] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
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
The eCos Hardware Abstraction Layer (HAL)
34
 
35
36
 
37
38
Introduction
39
40
This is an initial specification of the eCos Hardware Abstraction Layer (HAL). The HAL abstracts
42
the underlying hardware of a processor architecture and/or the
43
platform to a level sufficient for the eCos kernel to be ported onto
44
that platform.
45
46
 
47
48
Caveat
49
50
This document is an informal description of the HAL capabilities and
51
is not intended to be full documentation, although it may be used as a
52
source for such. It also describes the HAL as it is currently
53
implemented for the architectures targeted in this release. It most
54
closely describes the HALs for the MIPS, I386 and PowerPC HALs. Other
55
architectures are similar but may not be organized precisely as
56
described here.
57
58
59
 
60
61
 
62
63
64
 
65
66
Architecture, Variant and Platform
67
 
68
69
We have identified three levels at which the HAL must operate.
70
71
 
72
73
  
74
    
75
    The architecture
76
    HAL abstracts the basic CPU architecture and includes
77
    things like interrupt delivery, context switching, CPU startup
78
    etc.
79
    
80
  
81
 
82
  
83
    
84
    The  variant HAL
85
    encapsulates features of the CPU variant such as caches, MMU and
86
    FPU features. It also deals with any on-chip peripherals such as
87
    memory and interrupt controllers. For architectural variations,
88
    the actual implementation of the variation is often in the
89
    architectural HAL, and the variant HAL simply provides the correct
90
    configuration definitions.
91
    
92
  
93
 
94
    
95
    
96
    The platform HAL
97
    abstracts the properties of the current platform and includes
98
    things like platform startup, timer devices, I/O register access
99
    and interrupt controllers.
100
    
101
  
102
 
103
104
 
105
106
The boundaries between these three HAL levels are necessarily blurred
107
since functionality shifts between levels on a target-by-target basis.
108
For example caches and MMU may be either an architecture feature or a
109
variant feature. Similarly, memory and interrupt controllers may be
110
on-chip and in the variant HAL, or off-chip and in the platform HAL.
111
112
113
Generally there is a separate package for each of the architecture,
114
variant and package HALs for a target. For some of the older targets,
115
or where it would be essentially empty, the variant HAL is omitted.
116
117
118
 
119
120
121
 
122
123
General principles
124
 
125
126
The HAL has been implemented according to the following general
127
principles:
128
129
130
131
              The HAL is implemented in C and assembler, although the
132
              eCos kernel is largely implemented in C++.
133
              This is to permit the HAL the widest possible
134
              applicability.
135
136
137
All interfaces to the HAL are implemented by
138
              CPP macros. This allows them to be implemented as inline
139
              C code, inline assembler or function calls to external C
140
              or assembler code. This allows the most efficient
141
              implementation to be selected without affecting the
142
              interface. It also allows them to be redefined if the
143
              platform or variant HAL needs to replace or enhance a definition
144
              from the architecture HAL.
145
146
147
The HAL provides simple, portable mechanisms for dealing
148
with the hardware of a wide range of architectures and platforms.
149
It is always possible to bypass the HAL and program the hardware
150
directly, but this may lead to a loss of portability. 
151
152
153
154
 
155
156
157
 
158
159
<!-- <index></index> --><!-- <xref> -->HAL Interfaces
160
 
161
162
This section describes the main HAL interfaces.
163
164
 
165
166
 
167
168
Base Definitions
169
 
170
171
These are definitions that characterize the properties of the base
172
architecture that are used to compile the portable parts of the
173
kernel. They are concerned with such things a portable type
174
definitions, endianness, and labeling.
175
176
 
177
178
These definitions are supplied by the
179
cyg/hal/basetype.h header file which is supplied
180
by the architecture HAL. It is included automatically by
181
cyg/infra/cyg_type.h.
182
183
 
184
185
 
186
187
Byte order
188
189
  
190
    CYG_BYTEORDER
191
    
192
      
193
      This defines the byte order of the target and must be set to either
194
      CYG_LSBFIRST or CYG_MSBFIRST.
195
      
196
    
197
  
198
199
200
 
201
202
 
203
204
Label Translation
205
 
206
207
  
208
    CYG_LABEL_NAME(name)
209
    
210
 
211
      
212
      This is a wrapper used in some C and C++ files which
213
      use labels defined in assembly code or the linker script.
214
      It need only be defined if the default implementation in
215
      cyg/infra/cyg_type.h, which passes the name
216
      argument unaltered, is inadequate. It should be paired with
217
      CYG_LABEL_DEFN().
218
      
219
    
220
  
221
  
222
    CYG_LABEL_DEFN(name)
223
    
224
 
225
      
226
      This is a wrapper used in assembler sources and linker scripts
227
      which define labels.  It need only be defined if the default
228
      implementation in
229
      cyg/infra/cyg_type.h, which passes the name
230
      argument unaltered, is inadequate. The most usual alternative
231
      definition of this macro prepends an underscore to the label
232
      name.
233
      
234
    
235
  
236
237
238
 
239
 
240
241
 
242
243
Base types
244
245
        cyg_halint8
246
        cyg_halint16
247
        cyg_halint32
248
        cyg_halint64
249
        cyg_halcount8
250
        cyg_halcount16
251
        cyg_halcount32
252
        cyg_halcount64
253
        cyg_halbool
254
255
256
These macros define the C base types that should be used to define
257
variables of the given size. They only need to be defined if the
258
default types specified in cyg/infra/cyg_type.h
259
cannot be used. Note that these are only the base types, they will be
260
composed with signed and
261
unsigned to form full type specifications.
262
263
264
 
265
266
 
267
268
Atomic types
269
270
        cyg_halatomic CYG_ATOMIC
271
272
273
These types are guaranteed to be read or written in a single
274
uninterruptible operation. It is architecture defined what size this
275
type is, but it will be at least a byte.
276
277
278
 
279
280
 
281
282
283
 
284
285
Architecture Characterization
286
 
287
288
These are definition that are related to the basic architecture of the
289
CPU. These include the CPU context save format, context switching, bit
290
twiddling, breakpoints, stack sizes and address translation.
291
292
 
293
294
Most of these definition are found in
295
cyg/hal/hal_arch.h.  This file is supplied by the
296
architecture HAL. If there are variant or platform specific
297
definitions then these will be found in
298
cyg/hal/var_arch.h or
299
cyg/hal/plf_arch.h. These files are include
300
automatically by this header, so need not be included explicitly.
301
302
 
303
304
 
305
306
Register Save Format
307
308
typedef struct HAL_SavedRegisters
309
{
310
    /* architecture-dependent list of registers to be saved */
311
} HAL_SavedRegisters;
312
313
314
This structure describes the layout of a saved machine state on the
315
stack. Such states are saved during thread context switches,
316
interrupts and exceptions. Different quantities of state may be saved
317
during each of these, but usually a thread context state is a subset
318
of the interrupt state which is itself a subset of an exception state.
319
For debugging purposes, the same structure is used for all three
320
purposes, but where these states are significantly different, this
321
structure may contain a union of the three states.
322
323
324
 
325
 
326
327
 
328
329
Thread Context Initialization
330
 
331
332
HAL_THREAD_INIT_CONTEXT( sp, arg, entry, id )
333
334
 
335
336
This macro initializes a thread's context so that
337
it may be switched to by HAL_THREAD_SWITCH_CONTEXT().
338
The arguments are:
339
340
341
  
342
    sp
343
    
344
      
345
      A location containing the current value of the thread's stack
346
      pointer. This should be a variable or a structure field. The SP
347
      value will be read out of here and an adjusted value written
348
      back.
349
      
350
    
351
  
352
 
353
  
354
    arg
355
    
356
      
357
      A value that is passed as the first argument to the entry
358
      point function.
359
      
360
    
361
  
362
  
363
    entry
364
    
365
      
366
      The address of an entry point function. This will be called
367
      according the C calling conventions, and the value of
368
      arg will be passed as the first
369
      argument. This function should have the following type signature
370
      void entry(CYG_ADDRWORD arg).
371
      
372
    
373
  
374
  
375
    id
376
    
377
      
378
      A thread id value. This is only used for debugging purposes,
379
      it is ORed into the initialization pattern for unused registers
380
      and may be used to help identify the thread from its register dump.
381
      The least significant 16 bits of this value should be zero to allow
382
      space for a register identifier.
383
      
384
    
385
  
386
387
388
 
389
390
 
391
392
Thread Context Switching
393
 
394
395
HAL_THREAD_LOAD_CONTEXT( to )
396
HAL_THREAD_SWITCH_CONTEXT( from, to )
397
398
399
These macros implement the thread switch code. The arguments are:
400
401
 
402
403
  
404
    from
405
    
406
      
407
      A pointer to a location where the stack pointer of the current
408
      thread will be stored.
409
      
410
    
411
  
412
  
413
    to
414
    
415
      
416
      A pointer to a location from where the stack pointer of the next
417
      thread will be read.
418
      
419
    
420
  
421
422
 
423
424
For HAL_THREAD_LOAD_CONTEXT() the current CPU
425
state is discarded and the state of the destination thread is
426
loaded. This is only used once, to load the first thread when the
427
scheduler is started.
428
429
 
430
431
For HAL_THREAD_SWITCH_CONTEXT() the state of the
432
current thread is saved onto its stack, using the current value of the
433
stack pointer, and the address of the saved state placed in
434
*from.  The value in
435
*to is then read and the state of the new
436
thread is loaded from it.
437
438
 
439
440
While these two operations may be implemented with inline assembler,
441
they are normally implemented as calls to assembly code functions in
442
the HAL. There are two advantages to doing it this way. First, the
443
return link of the call provides a convenient PC value to be used in
444
the saved context. Second, the calling conventions mean that the
445
compiler will have already saved the caller-saved registers before the
446
call, so the HAL need only save the callee-saved registers.
447
448
 
449
450
The implementation of HAL_THREAD_SWITCH_CONTEXT()
451
saves the current CPU state on the stack, including the current
452
interrupt state (or at least the register that contains it). For
453
debugging purposes it is useful to save the entire register set, but
454
for performance only the ABI-defined callee-saved registers need be
455
saved. If it is implemented, the option
456
CYGDBG_HAL_COMMON_CONTEXT_SAVE_MINIMUM controls
457
how many registers are saved.
458
459
 
460
461
The implementation of HAL_THREAD_LOAD_CONTEXT()
462
loads a thread context, destroying the current context. With a little
463
care this can be implemented by sharing code with
464
HAL_THREAD_SWITCH_CONTEXT(). To load a thread
465
context simply requires the saved registers to be restored from the
466
stack and a jump or return made back to the saved PC.
467
468
 
469
470
Note that interrupts are not disabled during this process, any
471
interrupts that occur will be delivered onto the stack to which the
472
current CPU stack pointer points. Hence the stack pointer
473
should never be invalid, or loaded with a value that might cause the
474
saved state to become corrupted by an interrupt. However, the current
475
interrupt state is saved and restored as part of the thread
476
context. If a thread disables interrupts and does something to cause a
477
context switch, interrupts may be re-enabled on switching to another
478
thread. Interrupts will be disabled again when the original thread
479
regains control.
480
481
 
482
483
 
484
485
 
486
487
Bit indexing
488
 
489
490
HAL_LSBIT_INDEX( index, mask )
491
HAL_MSBIT_INDEX( index, mask )
492
493
 
494
495
These macros place in index the bit index of
496
the least significant bit in mask. Some
497
architectures have instruction level support for one or other of these
498
operations. If no architectural support is available, then these
499
macros may call C functions to do the job.
500
501
502
 
503
504
 
505
506
Idle thread activity
507
 
508
509
HAL_IDLE_THREAD_ACTION( count )
510
511
 
512
513
It may be necessary under some circumstances for the HAL to execute
514
code in the kernel idle thread's loop. An example might be to execute
515
a processor halt instruction. This macro provides a portable way of
516
doing this. The argument is a copy of the idle thread's loop counter,
517
and may be used to trigger actions at longer intervals than every
518
loop.
519
520
521
 
522
523
 
524
525
Reorder barrier
526
 
527
528
HAL_REORDER_BARRIER()
529
530
 
531
532
When optimizing the compiler can reorder code. In some parts of
533
multi-threaded systems, where the order of actions is vital, this can
534
sometimes cause problems. This macro may be inserted into places where
535
reordering should not happen and prevents code being migrated across
536
it by the compiler optimizer. It should be placed between statements
537
that must be executed in the order written in the code.
538
539
540
 
541
542
 
543
544
Breakpoint support
545
 
546
547
HAL_BREAKPOINT( label )
548
HAL_BREAKINST
549
HAL_BREAKINST_SIZE
550
551
 
552
553
These macros provide support for breakpoints.
554
555
 
556
557
HAL_BREAKPOINT() executes a breakpoint
558
instruction. The label is defined at the breakpoint instruction so
559
that exception code can detect which breakpoint was executed.
560
561
 
562
563
HAL_BREAKINST contains the breakpoint instruction
564
code as an integer value. HAL_BREAKINST_SIZE is
565
the size of that breakpoint instruction in bytes. Together these
566
may be used to place a breakpoint in any code.
567
568
569
 
570
571
 
572
573
GDB support
574
 
575
576
HAL_THREAD_GET_SAVED_REGISTERS( sp, regs )
577
HAL_GET_GDB_REGISTERS( regval, regs )
578
HAL_SET_GDB_REGISTERS( regs, regval )
579
580
 
581
582
These macros provide support for interfacing GDB to the HAL.
583
584
 
585
586
HAL_THREAD_GET_SAVED_REGISTERS() extracts a
587
pointer to a HAL_SavedRegisters structure
588
from a stack pointer value. The stack pointer passed in should be the
589
value saved by the thread context macros. The macro will assign a
590
pointer to the HAL_SavedRegisters structure
591
to the variable passed as the second argument.
592
593
 
594
595
HAL_GET_GDB_REGISTERS() translates a register
596
state as saved by the HAL and into a register dump in the format
597
expected by GDB. It takes a pointer to a
598
HAL_SavedRegisters structure in the
599
regs argument and a pointer to the memory to
600
contain the GDB register dump in the regval
601
argument.
602
603
 
604
605
HAL_SET_GDB_REGISTERS() translates a GDB format
606
register dump into a the format expected by the HAL.  It takes a
607
pointer to the memory containing the GDB register dump in the
608
regval argument and a pointer to a
609
HAL_SavedRegisters structure
610
in the regs argument.
611
612
613
 
614
615
 
616
617
Setjmp and longjmp support
618
 
619
620
CYGARC_JMP_BUF_SIZE
621
hal_jmp_buf[CYGARC_JMP_BUF_SIZE]
622
hal_setjmp( hal_jmp_buf env )
623
hal_longjmp( hal_jmp_buf env, int val )
624
625
 
626
627
These functions provide support for the C
628
setjmp() and longjmp()
629
functions.  Refer to the C library for further information.
630
631
 
632
633
 
634
635
 
636
637
Stack Sizes
638
639
CYGNUM_HAL_STACK_SIZE_MINIMUM
640
CYGNUM_HAL_STACK_SIZE_TYPICAL
641
642
 
643
644
The values of these macros define the minimum and typical sizes of
645
thread stacks.
646
647
 
648
649
CYGNUM_HAL_STACK_SIZE_MINIMUM defines the minimum
650
size of a thread stack. This is enough for the thread to function
651
correctly within eCos and allows it to take interrupts and context
652
switches. There should also be enough space for a simple thread entry
653
function to execute and call basic kernel operations on objects like
654
mutexes and semaphores. However there will not be enough room for much
655
more than this. When creating stacks for their own threads,
656
applications should determine the stack usage needed for application
657
purposes and then add
658
CYGNUM_HAL_STACK_SIZE_MINIMUM.
659
660
 
661
662
CYGNUM_HAL_STACK_SIZE_TYPICAL is a reasonable increment over
663
CYGNUM_HAL_STACK_SIZE_MINIMUM, usually about 1kB. This should be
664
adequate for most modest thread needs. Only threads that need to
665
define significant amounts of local data, or have very deep call trees
666
should need to use a larger stack size.
667
668
 
669
670
 
671
 
672
673
 
674
675
Address Translation
676
 
677
678
CYGARC_CACHED_ADDRESS(addr)
679
CYGARC_UNCACHED_ADDRESS(addr)
680
CYGARC_PHYSICAL_ADDRESS(addr)
681
682
 
683
684
These macros provide address translation between different views of
685
memory. In many architectures a given memory location may be visible
686
at different addresses in both cached and uncached forms. It is also
687
possible that the MMU or some other address translation unit in the
688
CPU presents memory to the program at a different virtual address to
689
its physical address on the bus.
690
691
 
692
693
CYGARC_CACHED_ADDRESS() translates the given
694
address to its location in cached memory. This is typically where the
695
application will access the memory.
696
697
 
698
699
CYGARC_UNCACHED_ADDRESS() translates the given
700
address to its location in uncached memory. This is typically where
701
device drivers will access the memory to avoid cache problems. It may
702
additionally be necessary for the cache to be flushed before the
703
contents of this location is fully valid.
704
705
 
706
707
CYGARC_PHYSICAL_ADDRESS() translates the given
708
address to its location in the physical address space. This is
709
typically the address that needs to be passed to device hardware such
710
as a DMA engine, ethernet device or PCI bus bridge. The physical
711
address may not be directly accessible to the program, it may be
712
re-mapped by address translation.
713
714
 
715
716
 
717
 
718
719
 
720
721
Global Pointer
722
 
723
724
CYGARC_HAL_SAVE_GP()
725
CYGARC_HAL_RESTORE_GP()
726
727
 
728
729
These macros insert code to save and restore any global data pointer
730
that the ABI uses. These are necessary when switching context between
731
two eCos instances - for example between an eCos application and
732
RedBoot.
733
734
 
735
736
 
737
738
 
739
740
741
 
742
743
Interrupt Handling
744
 
745
746
These interfaces contain definitions related to interrupt
747
handling. They include definitions of exception and interrupt numbers,
748
interrupt enabling and masking, and realtime clock operations.
749
750
 
751
752
These definitions are normally found in
753
cyg/hal/hal_intr.h.  This file is supplied by the
754
architecture HAL.  Any variant or platform specific definitions will
755
be found in cyg/hal/var_intr.h,
756
cyg/hal/plf_intr.h or
757
cyg/hal/hal_platform_ints.h in the variant or platform
758
HAL, depending on the exact target. These files are include
759
automatically by this header, so need not be included explicitly.
760
761
 
762
763
 
764
765
Vector numbers
766
 
767
768
CYGNUM_HAL_VECTOR_XXXX
769
CYGNUM_HAL_VSR_MIN
770
CYGNUM_HAL_VSR_MAX
771
CYGNUM_HAL_VSR_COUNT
772
 
773
CYGNUM_HAL_INTERRUPT_XXXX
774
CYGNUM_HAL_ISR_MIN
775
CYGNUM_HAL_ISR_MAX
776
CYGNUM_HAL_ISR_COUNT
777
 
778
CYGNUM_HAL_EXCEPTION_XXXX
779
CYGNUM_HAL_EXCEPTION_MIN
780
CYGNUM_HAL_EXCEPTION_MAX
781
CYGNUM_HAL_EXCEPTION_COUNT
782
783
 
784
785
All possible VSR, interrupt and exception vectors are specified here,
786
together with maximum and minimum values for range checking. While the
787
VSR and exception numbers will be defined in this file, the interrupt
788
numbers will normally be defined in the variant or platform HAL file
789
that is included by this header.
790
791
 
792
793
There are two ranges of numbers, those for the vector service
794
routines and those for the interrupt service routines. The relationship
795
between these two ranges is undefined, and no equivalence should
796
be assumed if vectors from the two ranges coincide.
797
798
 
799
800
The VSR vectors correspond to the set of exception vectors that can be
801
delivered by the CPU architecture, many of these will be internal
802
exception traps. The ISR vectors correspond to the set of external
803
interrupts that can be delivered and are usually determined by extra
804
decoding of the interrupt controller by the interrupt VSR.
805
806
 
807
808
Where a CPU supports synchronous exceptions, the range of such
809
exceptions allowed are defined by CYGNUM_HAL_EXCEPTION_MIN and
810
CYGNUM_HAL_EXCEPTION_MAX. The
811
CYGNUM_HAL_EXCEPTION_XXXX definitions are
812
standard names used by target independent code to test for the
813
presence of particular exceptions in the architecture. The actual
814
exception numbers will normally correspond to the VSR exception
815
range. In future other exceptions generated by the system software
816
(such as stack overflow) may be added.
817
818
 
819
820
CYGNUM_HAL_ISR_COUNT, CYGNUM_HAL_VSR_COUNT and
821
CYGNUM_HAL_EXCEPTION_COUNT define the number of
822
ISRs, VSRs and EXCEPTIONs respectively for the purposes of defining
823
arrays etc. There might be a translation from the supplied vector
824
numbers into array offsets. Hence
825
CYGNUM_HAL_XXX_COUNT may not simply be
826
CYGNUM_HAL_XXX_MAX - CYGNUM_HAL_XXX_MIN or CYGNUM_HAL_XXX_MAX+1.
827
828
 
829
830
 
831
 
832
833
 
834
835
Interrupt state control
836
 
837
838
CYG_INTERRUPT_STATE
839
HAL_DISABLE_INTERRUPTS( old )
840
HAL_RESTORE_INTERRUPTS( old )
841
HAL_ENABLE_INTERRUPTS()
842
HAL_QUERY_INTERRUPTS( state )
843
844
 
845
846
These macros provide control over the state of the CPUs interrupt mask
847
mechanism. They should normally manipulate a CPU status register to
848
enable and disable interrupt delivery. They should not access an
849
interrupt controller.
850
851
 
852
 
853
854
CYG_INTERRUPT_STATE is a data type that should be
855
used to store the interrupt state returned by
856
HAL_DISABLE_INTERRUPTS() and
857
HAL_QUERY_INTERRUPTS() and passed to
858
HAL_RESTORE_INTERRUPTS().
859
860
 
861
862
HAL_DISABLE_INTERRUPTS() disables the delivery of
863
interrupts and stores the original state of the interrupt mask in the
864
variable passed in the old argument.
865
866
 
867
868
HAL_RESTORE_INTERRUPTS() restores the state of
869
the interrupt mask to that recorded in old.
870
871
 
872
873
HAL_ENABLE_INTERRUPTS() simply enables interrupts
874
regardless of the current state of the mask.
875
876
 
877
878
HAL_QUERY_INTERRUPTS() stores the state of the
879
interrupt mask in the variable passed in the 
880
state argument. The state stored here should also be
881
capable of being passed to
882
HAL_RESTORE_INTERRUPTS() at a later point.
883
884
 
885
886
It is at the HAL implementer’s discretion exactly
887
which interrupts are masked by this mechanism. Where a CPU has more
888
than one interrupt type that may be masked separately (e.g. the
889
ARM's IRQ and FIQ) only those that can raise DSRs need
890
to be masked here. A separate architecture specific mechanism may
891
then be used to control the other interrupt types.
892
893
 
894
895
 
896
897
 
898
899
ISR and VSR management
900
 
901
902
HAL_INTERRUPT_IN_USE( vector, state )
903
HAL_INTERRUPT_ATTACH( vector, isr, data, object )
904
HAL_INTERRUPT_DETACH( vector, isr )
905
HAL_VSR_SET( vector, vsr, poldvsr )
906
HAL_VSR_GET( vector, pvsr )
907
HAL_VSR_SET_TO_ECOS_HANDLER( vector, poldvsr )
908
909
 
910
911
These macros manage the attachment of interrupt and vector service
912
routines to interrupt and exception vectors respectively.
913
914
 
915
916
HAL_INTERRUPT_IN_USE() tests the state of the
917
supplied interrupt vector and sets the value of the state parameter to
918
either 1 or 0 depending on whether there is already an ISR attached to
919
the vector. The HAL will only allow one ISR to be attached to each
920
vector, so it is a good idea to use this function before using
921
HAL_INTERRUPT_ATTACH().
922
923
 
924
925
HAL_INTERRUPT_ATTACH() attaches
926
the ISR, data pointer and object pointer to the given
927
vector. When an interrupt occurs on this
928
vector the ISR is called using the C calling convention and the vector
929
number and data pointer are passed to it as the first and second
930
arguments respectively.
931
932
 
933
934
HAL_INTERRUPT_DETACH() detaches the ISR from the
935
vector.
936
937
 
938
939
HAL_VSR_SET() replaces the VSR attached to the
940
vector with the replacement supplied in
941
vsr. The old VSR is returned in the location
942
pointed to by pvsr.
943
944
 
945
946
HAL_VSR_GET() assigns
947
a copy of the VSR to the location pointed to by pvsr.
948
949
 
950
951
HAL_VSR_SET_TO_ECOS_HANDLER() ensures that the
952
VSR for a specific exception is pointing at the eCos exception VSR and
953
not one for RedBoot or some other ROM monitor. The default when
954
running under RedBoot is for exceptions to be handled by RedBoot and
955
passed to GDB. This macro diverts the exception to eCos so that it may
956
be handled by application code. The arguments are the VSR vector to be
957
replaces, and a location in which to store the old VSR pointer, so
958
that it may be replaced at a later point.
959
960
 
961
962
 
963
964
 
965
966
Interrupt controller management
967
 
968
969
HAL_INTERRUPT_MASK( vector )
970
HAL_INTERRUPT_UNMASK( vector )
971
HAL_INTERRUPT_ACKNOWLEDGE( vector )
972
HAL_INTERRUPT_CONFIGURE( vector, level, up )
973
HAL_INTERRUPT_SET_LEVEL( vector, level )
974
975
 
976
977
These macros exert control over any prioritized interrupt
978
controller that is present. If no priority controller exists, then
979
these macros should be empty.
980
981
 
982
983
  
984
  These macros may not be reentrant, so care should be taken to
985
  prevent them being called while interrupts are enabled. This means
986
  that they can be safely used in initialization code before
987
  interrupts are enabled, and in ISRs. In DSRs, ASRs and thread code,
988
  however, interrupts must be disabled before these macros are
989
  called. Here is an example for use in a DSR where the interrupt
990
  source is unmasked after data processing:
991
  
992
993
 ...
994
 HAL_DISABLE_INTERRUPTS(old);
995
 HAL_INTERRUPT_UNMASK(CYGNUM_HAL_INTERRUPT_ETH);
996
 HAL_RESTORE_INTERRUPTS(old);
997
 ...
998
999
1000
 
1001
1002
HAL_INTERRUPT_MASK() causes the interrupt
1003
associated with the given vector to be blocked.
1004
1005
 
1006
1007
HAL_INTERRUPT_UNMASK() causes the interrupt
1008
associated with the given vector to be unblocked.
1009
1010
 
1011
1012
HAL_INTERRUPT_ACKNOWLEDGE() acknowledges the
1013
current interrupt from the given vector. This is usually executed from
1014
the ISR for this vector when it is prepared to allow further
1015
interrupts.  Most interrupt controllers need some form of acknowledge
1016
action before the next interrupt is allowed through. Executing this
1017
macro may cause another interrupt to be delivered. Whether this
1018
interrupts the current code depends on the state of the CPU interrupt
1019
mask.
1020
1021
 
1022
1023
HAL_INTERRUPT_CONFIGURE() provides
1024
control over how an interrupt signal is detected. The arguments
1025
are:
1026
1027
1028
  
1029
    vector
1030
    
1031
      The interrupt vector to be configured.
1032
    
1033
  
1034
 
1035
  
1036
    level
1037
    
1038
      
1039
      Set to true if the interrupt is detected by
1040
      level, and false if it is edge triggered.
1041
      
1042
    
1043
  
1044
 
1045
  
1046
    up
1047
    
1048
      
1049
      If the interrupt is set to level detect, then if this is
1050
      true it is detected by a high signal level,
1051
      and if false by a low signal level. If the
1052
      interrupt is set to edge triggered, then if this is
1053
      true it is triggered by a rising edge and if
1054
      false by a falling edge.
1055
      
1056
    
1057
  
1058
1059
 
1060
1061
HAL_INTERRUPT_SET_LEVEL() provides control over
1062
the hardware priority of the interrupt. The arguments are:
1063
1064
 
1065
1066
  
1067
    vector
1068
    
1069
      The interrupt whose level is to be set.
1070
    
1071
  
1072
 
1073
  
1074
    level
1075
    
1076
      
1077
      The priority level to which the interrupt is to set. In some
1078
      architectures the masking of an interrupt is achieved by
1079
      changing its priority level. Hence this function,
1080
      HAL_INTERRUPT_MASK() and
1081
      HAL_INTERRUPT_UNMASK() may interfere with
1082
      each other.
1083
      
1084
    
1085
  
1086
1087
 
1088
1089
 
1090
1091
 
1092
1093
Clock control
1094
 
1095
1096
HAL_CLOCK_INITIALIZE( period )
1097
HAL_CLOCK_RESET( vector, period )
1098
HAL_CLOCK_READ( pvalue )
1099
1100
 
1101
1102
These macros provide control over a clock or timer device that may be
1103
used by the kernel to provide time-out, delay and scheduling
1104
services. The clock is assumed to be implemented by some form of
1105
counter that is incremented or decremented by some external source and
1106
which raises an interrupt when it reaches a predetermined value.
1107
1108
 
1109
1110
HAL_CLOCK_INITIALIZE() initializes the timer
1111
device to interrupt at the given period. The period is essentially the
1112
value used to initialize the timer counter and must be calculated from
1113
the timer frequency and the desired interrupt rate. The timer device
1114
should generate an interrupt every period cycles.
1115
1116
 
1117
1118
HAL_CLOCK_RESET() re-initializes the timer to
1119
provoke the next interrupt. This macro is only really necessary when
1120
the timer device needs to be reset in some way after each interrupt.
1121
1122
 
1123
1124
HAL_CLOCK_READ() reads the current value of the
1125
timer counter and puts the value in the location pointed to by
1126
pvalue. The value stored will always be the
1127
number of timer cycles since the last interrupt, and hence ranges
1128
between zero and the initial period value. If this is a count-down
1129
cyclic timer, some arithmetic may be necessary to generate this value.
1130
1131
 
1132
1133
 
1134
1135
 
1136
1137
Microsecond Delay
1138
 
1139
1140
HAL_DELAY_US(us)
1141
1142
 
1143
1144
This is an optional definition. If defined the macro implements a busy
1145
loop delay for the given number of microseconds. This is usually
1146
implemented by waiting for the required number of hardware timer ticks
1147
to pass.
1148
1149
 
1150
1151
This operation should normally be used when a very short delay is
1152
needed when controlling hardware, programming FLASH devices and similar
1153
situations where a wait/timeout loop would otherwise be used. Since it
1154
may disable interrupts, and is implemented by busy waiting, it should
1155
not be used in code that is sensitive to interrupt or context switch
1156
latencies.
1157
1158
 
1159
1160
 
1161
1162
 
1163
1164
1165
 
1166
1167
HAL I/O
1168
 
1169
1170
This section contains definitions for supporting access
1171
to device control registers in an architecture neutral
1172
fashion.
1173
1174
 
1175
1176
These definitions are normally found in the header file
1177
cyg/hal/hal_io.h.  This file itself contains
1178
macros that are generic to the architecture. If there are variant or
1179
platform specific IO access macros then these will be found in
1180
cyg/hal/var_io.h and
1181
cyg/hal/plf_io.h in the variant or platform HALs
1182
respectively. These files are include automatically by this header, so
1183
need not be included explicitly.
1184
1185
 
1186
1187
This header (or more likely cyg/hal/plf_io.h) also
1188
defines the PCI access macros. For more information on these see 
1189
linkend="pci-library-reference">.
1190
1191
 
1192
1193
 
1194
1195
Register address
1196
 
1197
1198
HAL_IO_REGISTER
1199
1200
 
1201
1202
This type is used to store the address of an I/O register. It will
1203
normally be a memory address, an integer port address or an offset
1204
into an I/O space. More complex architectures may need to code an
1205
address space plus offset pair into a single word, or may represent it
1206
as a structure.
1207
1208
 
1209
1210
Values of variables and constants of this type will usually be
1211
supplied by configuration mechanisms or in target specific headers.
1212
1213
 
1214
1215
 
1216
1217
 
1218
1219
Register read
1220
 
1221
1222
HAL_READ_XXX( register, value )
1223
HAL_READ_XXX_VECTOR( register, buffer, count, stride )
1224
1225
 
1226
1227
These macros support the reading of I/O registers in various
1228
sizes. The XXX component of the name may be
1229
UINT8, UINT16,
1230
UINT32.
1231
1232
 
1233
1234
HAL_READ_XXX() reads the appropriately sized
1235
value from the register and stores it in the variable passed as the
1236
second argument.
1237
1238
 
1239
1240
HAL_READ_XXX_VECTOR() reads
1241
count values of the appropriate size into
1242
buffer. The stride
1243
controls how the pointer advances through the register space. A stride
1244
of zero will read the same register repeatedly, and a stride of one
1245
will read adjacent registers of the given size. Greater strides will
1246
step by larger amounts, to allow for sparsely mapped registers for
1247
example.
1248
1249
 
1250
1251
 
1252
1253
Register write
1254
 
1255
1256
HAL_WRITE_XXX( register, value )
1257
HAL_WRITE_XXX_VECTOR( register, buffer,count, stride )
1258
1259
 
1260
1261
These macros support the writing of I/O registers in various
1262
sizes. The XXX component of the name may be
1263
UINT8, UINT16,
1264
UINT32.
1265
1266
 
1267
1268
HAL_WRITE_XXX() writes
1269
the appropriately sized value from the variable passed as the second argument
1270
stored it in the register.
1271
HAL_WRITE_XXX_VECTOR() writes
1272
count values of the appropriate size from 
1273
buffer. The stride controls
1274
how the pointer advances through the register space. A stride of
1275
zero will write the same register repeatedly, and a stride of one
1276
will write adjacent registers of the given size. Greater strides
1277
will step by larger amounts, to allow for sparsely mapped registers
1278
for example.
1279
1280
1281
 
1282
1283
1284
 
1285
1286
Cache Control
1287
 
1288
This section contains definitions for supporting control
1289
of the caches on the CPU.
1290
1291
 
1292
1293
These definitions are usually found in the header file
1294
cyg/hal/hal_cache.h.  This file may be defined in
1295
the architecture, variant or platform HAL, depending on where the
1296
caches are implemented for the target. Often there will be a generic
1297
implementation of the cache control macros in the architecture HAL
1298
with the ability to override or undefine them in the variant or
1299
platform HAL. Even when the implementation of the cache macros is in
1300
the architecture HAL, the cache dimensions will be defined in the
1301
variant or platform HAL. As with other files, the variant or platform
1302
specific definitions are usually found in
1303
cyg/hal/var_cache.h and
1304
cyg/hal/plf_cache.h respectively.  These files
1305
are include automatically by this header, so need not be included
1306
explicitly.
1307
1308
 
1309
1310
There are versions of the macros defined here for both the Data and
1311
Instruction caches. these are distinguished by the use of either
1312
DCACHE or ICACHE in the macro
1313
names. Some architectures have a unified cache, where both data and
1314
instruction share the same cache. In these cases the control macros
1315
use UCACHE and the DCACHE and
1316
ICACHE macros will just be calls to the
1317
UCACHE version. In the following descriptions,
1318
XCACHE is used to stand for any of these. Where
1319
there are issues specific to a particular cache, this will be
1320
explained in the text.
1321
1322
 
1323
1324
There might be target specific restrictions on the use of some of the
1325
macros which it is the user's responsibility to comply with. Such
1326
restrictions are documented in the header file with the macro
1327
definition.
1328
1329
 
1330
1331
Note that destructive cache macros should be used with caution.
1332
Preceding a cache invalidation with a cache synchronization is not
1333
safe in itself since an interrupt may happen after the synchronization
1334
but before the invalidation. This might cause the state of dirty data
1335
lines created during the interrupt to be lost.
1336
1337
 
1338
1339
Depending on the architecture's capabilities, it may be possible to
1340
temporarily disable the cache while doing the synchronization and
1341
invalidation which solves the problem (no new data would be cached
1342
during an interrupt). Otherwise it is necessary to disable interrupts
1343
while manipulating the cache which may take a long time.
1344
1345
 
1346
1347
Some platform HALs now support a pair of cache state query
1348
macros: HAL_ICACHE_IS_ENABLED( x ) and
1349
HAL_DCACHE_IS_ENABLED( x ) which set the argument
1350
to true if the instruction or data cache is enabled,
1351
respectively. Like most cache control macros, these are optional,
1352
because the capabilities of different targets and boards can vary
1353
considerably. Code which uses them, if it is to be considered
1354
portable, should test for their existence first by means of
1355
#ifdef.  Be sure to include
1356
<cyg/hal/hal_cache.h> in order to do this
1357
test and (maybe) use the macros.
1358
1359
 
1360
1361
 
1362
1363
Cache Dimensions
1364
 
1365
1366
HAL_XCACHE_SIZE
1367
HAL_XCACHE_LINE_SIZE
1368
HAL_XCACHE_WAYS
1369
HAL_XCACHE_SETS
1370
1371
1372
These macros define the size and dimensions of the Instruction
1373
and Data caches.
1374
1375
 
1376
1377
  
1378
    HAL_XCACHE_SIZE     
1379
    
1380
      Defines the total size of the cache in bytes.
1381
    
1382
  
1383
 
1384
  
1385
    HAL_XCACHE_LINE_SIZE        
1386
    
1387
      Defines the cache line size in bytes.
1388
    
1389
  
1390
 
1391
  
1392
    HAL_XCACHE_WAYS     
1393
    
1394
      
1395
      Defines the number of ways in each set and defines its level
1396
      of associativity. This would be 1 for a direct mapped
1397
      cache, 2 for a 2-way cache, 4 for 4-way and so on.
1398
      
1399
    
1400
  
1401
 
1402
  
1403
    HAL_XCACHE_SETS     
1404
    
1405
      
1406
      Defines the number of sets in the cache, and is calculated from
1407
      the previous values.
1408
      
1409
    
1410
  
1411
1412
 
1413
1414
 
1415
1416
 
1417
1418
Global Cache Control
1419
 
1420
1421
HAL_XCACHE_ENABLE()
1422
HAL_XCACHE_DISABLE()
1423
HAL_XCACHE_INVALIDATE_ALL()
1424
HAL_XCACHE_SYNC()
1425
HAL_XCACHE_BURST_SIZE( size )
1426
HAL_DCACHE_WRITE_MODE( mode )
1427
HAL_XCACHE_LOCK( base, size )
1428
HAL_XCACHE_UNLOCK( base, size )
1429
HAL_XCACHE_UNLOCK_ALL()
1430
1431
 
1432
1433
These macros affect the state of the entire cache, or a large part of
1434
it.
1435
1436
 
1437
1438
  
1439
    HAL_XCACHE_ENABLE() and HAL_XCACHE_DISABLE()
1440
    
1441
      Enable and disable the cache.
1442
    
1443
  
1444
 
1445
  
1446
    HAL_XCACHE_INVALIDATE_ALL()
1447
    
1448
      
1449
      Causes the entire contents of the cache to be invalidated.
1450
      Depending on the hardware, this may require the cache to be disabled
1451
      during the invalidation process. If so, the implementation must
1452
      use HAL_XCACHE_IS_ENABLED() to save and
1453
      restore the previous state.
1454
      
1455
      
1456
        
1457
        If this macro is called after
1458
        HAL_XCACHE_SYNC() with the intention of clearing
1459
        the cache (invalidating the cache after writing dirty data back to
1460
        memory), you must prevent interrupts from happening between the two
1461
        calls:
1462
        
1463
1464
 ...
1465
 HAL_DISABLE_INTERRUPTS(old);
1466
 HAL_XCACHE_SYNC();
1467
 HAL_XCACHE_INVALIDATE_ALL();
1468
 HAL_RESTORE_INTERRUPTS(old);
1469
 ...
1470
1471
        
1472
        Since the operation may take a very long time, real-time
1473
        responsiveness could be affected, so only do this when it is
1474
        absolutely required and you know the delay will not interfere
1475
        with the operation of drivers or the application.
1476
        
1477
      
1478
    
1479
  
1480
 
1481
  
1482
    HAL_XCACHE_SYNC()
1483
    
1484
      
1485
      Causes the contents of the cache to be brought into synchronization
1486
      with the contents of memory. In some implementations this may be
1487
      equivalent to HAL_XCACHE_INVALIDATE_ALL().
1488
      
1489
    
1490
  
1491
 
1492
  
1493
    HAL_XCACHE_BURST_SIZE()
1494
    
1495
      
1496
      Allows the size of cache to/from memory bursts to
1497
      be controlled. This macro will only be defined if this functionality
1498
      is available.
1499
      
1500
    
1501
  
1502
 
1503
  
1504
    HAL_DCACHE_WRITE_MODE()
1505
    
1506
      
1507
      Controls the way in which data cache lines are written back to
1508
      memory. There will be definitions for the possible
1509
      modes. Typical definitions are
1510
      HAL_DCACHE_WRITEBACK_MODE and
1511
      HAL_DCACHE_WRITETHRU_MODE. This macro will
1512
      only be defined if this functionality is available.
1513
      
1514
    
1515
  
1516
 
1517
  
1518
    HAL_XCACHE_LOCK()
1519
    
1520
      
1521
      Causes data to be locked into the cache. The base and size
1522
      arguments define the memory region that will be locked into the
1523
      cache. It is architecture dependent whether more than one locked
1524
      region is allowed at any one time, and whether this operation
1525
      causes the cache to cease acting as a cache for addresses
1526
      outside the region during the duration of the lock. This macro
1527
      will only be defined if this functionality is available.
1528
      
1529
    
1530
  
1531
 
1532
  
1533
    HAL_XCACHE_UNLOCK()
1534
    
1535
      
1536
      Cancels the locking of the memory region given. This should
1537
      normally correspond to a region supplied in a matching lock
1538
      call.  This macro will only be defined if this functionality is
1539
      available.
1540
      
1541
    
1542
  
1543
 
1544
  
1545
    HAL_XCACHE_UNLOCK_ALL()
1546
    
1547
      
1548
      Cancels all existing locked memory regions. This may be required
1549
      as part of the cache initialization on some architectures. This
1550
      macro will only be defined if this functionality is available.
1551
      
1552
    
1553
  
1554
1555
 
1556
1557
 
1558
1559
 
1560
1561
Cache Line Control
1562
 
1563
1564
HAL_DCACHE_ALLOCATE( base , size )
1565
HAL_DCACHE_FLUSH( base , size )
1566
HAL_XCACHE_INVALIDATE( base , size )
1567
HAL_DCACHE_STORE( base , size )
1568
HAL_DCACHE_READ_HINT( base , size )
1569
HAL_DCACHE_WRITE_HINT( base , size )
1570
HAL_DCACHE_ZERO( base , size )
1571
1572
 
1573
1574
All of these macros apply a cache operation to all cache lines that
1575
match the memory address region defined by the base and size
1576
arguments. These macros will only be defined if the described
1577
functionality is available. Also, it is not guaranteed that the cache
1578
function will only be applied to just the described regions, in some
1579
architectures it may be applied to the whole cache.
1580
1581
 
1582
1583
  
1584
    HAL_DCACHE_ALLOCATE()
1585
    
1586
      
1587
      Allocates lines in the cache for the given region without
1588
      reading their contents from memory, hence the contents of the lines
1589
      is undefined. This is useful for preallocating lines which are to
1590
      be completely overwritten, for example in a block copy
1591
      operation.
1592
      
1593
    
1594
  
1595
 
1596
  
1597
    HAL_DCACHE_FLUSH()
1598
    
1599
      
1600
      Invalidates all cache lines in the region after writing any
1601
      dirty lines to memory.
1602
      
1603
    
1604
  
1605
 
1606
  
1607
    HAL_XCACHE_INVALIDATE() 
1608
    
1609
      
1610
      Invalidates all cache lines in the region. Any dirty lines
1611
      are invalidated without being written to memory.
1612
      
1613
    
1614
  
1615
 
1616
  
1617
    HAL_DCACHE_STORE() 
1618
    
1619
      
1620
      Writes all dirty lines in the region to memory, but does not
1621
      invalidate any lines.
1622
      
1623
    
1624
  
1625
 
1626
  
1627
    HAL_DCACHE_READ_HINT() 
1628
    
1629
      
1630
      Hints to the cache that the region is going to be read from
1631
      in the near future. This may cause the region to be speculatively
1632
      read into the cache.
1633
      
1634
    
1635
  
1636
 
1637
  
1638
    HAL_DCACHE_WRITE_HINT() 
1639
    
1640
      
1641
      Hints to the cache that the region is going to be written
1642
      to in the near future. This may have the identical behavior to
1643
      HAL_DCACHE_READ_HINT().
1644
      
1645
    
1646
  
1647
 
1648
  
1649
    HAL_DCACHE_ZERO()
1650
    
1651
      
1652
      Allocates and zeroes lines in the cache for the given
1653
      region without reading memory. This is useful if a large area of
1654
      memory is to be cleared.
1655
      
1656
    
1657
  
1658
1659
 
1660
1661
1662
 
1663
1664
1665
 
1666
1667
<!-- <xref> -->Linker Scripts
1668
 
1669
1670
When an eCos application is linked it must be done under the control
1671
of a linker script. This script defines the memory areas, addresses
1672
and sized, into which the code and data are to be put, and allocates
1673
the various sections generated by the compiler to these.
1674
1675
 
1676
1677
The linker script actually used is in
1678
lib/target.ld in the install directory. This is
1679
actually manufactured out of two other files: a base linker script and
1680
an .ldi file that was generated by the memory
1681
layout tool.
1682
1683
 
1684
1685
The base linker script is usually supplied either by the architecture
1686
HAL or the variant HAL. It consists of a set of linker script
1687
fragments, in the form of C preprocessor macros, that define the major
1688
output sections to be generated by the link operation. The
1689
.ldi file, which is #include'ed
1690
by the base linker script, uses these macro definitions to assign the
1691
output sections to the required memory areas and link addresses.
1692
1693
 
1694
1695
The .ldi file is supplied by the platform HAL, and
1696
contains knowledge of the memory layout of the target platform. These
1697
files generally conform to a standard naming convention, each file
1698
being of the form:
1699
1700
1701
pkgconf/mlt_<architecture>_<variant>_<platform>_<startup>.ldi
1702
1703
1704
where <architecture>,
1705
<variant> and
1706
<platform> are the respective HAL package
1707
names and <startup> is the startup type which
1708
is usually one of ROM, RAM or
1709
ROMRAM.
1710
1711
 
1712
1713
In addition to the .ldi file, there is also a
1714
congruously name .h file. This may be used by the
1715
application to access information defined in the
1716
.ldi file. Specifically it contains the memory
1717
layout defined there, together with any additional section names
1718
defined by the user. Examples of the latter are heap areas or PCI bus
1719
memory access windows.
1720
1721
 
1722
1723
The .ldi is manufactured by the Memory
1724
Layout Tool (MLT). The MLT saves the memory
1725
configuration into a file named
1726
1727
1728
include/pkgconf/mlt_<architecture>_<variant>_<platform>_<startup>.mlt
1729
1730
1731
in the platform HAL. This file is used by the
1732
MLT to manufacture both the
1733
.ldi and .h files. Users should
1734
beware that direct edits the either of these files may be overwritten
1735
if the MLT is run and regenerates them from the
1736
.mlt file.
1737
1738
 
1739
1740
The names of the .ldi and .h
1741
files are defined by macro definitions in
1742
pkgconf/system.h. These are
1743
CYGHWR_MEMORY_LAYOUT_LDI and
1744
CYGHWR_MEMORY_LAYOUT_H respectively. While there
1745
will be little need for the application to refer to the
1746
.ldi file directly, it may include the
1747
.h file as follows:
1748
1749
 
1750
1751
#include CYGHWR_MEMORY_LAYOUT_H
1752
1753
 
1754
1755
 
1756
1757
1758
 
1759
1760
Diagnostic Support
1761
 
1762
1763
The HAL provides support for low level diagnostic IO. This is
1764
particularly useful during early development as an aid to bringing up
1765
a new platform. Usually this diagnostic channel is a UART or some
1766
other serial IO device, but it may equally be a a memory
1767
buffer, a simulator supported output channel, a ROM emulator virtual
1768
UART, and LCD panel, a memory mapped video buffer or any other output
1769
device.
1770
1771
 
1772
1773
HAL_DIAG_INIT() performs any initialization
1774
required on the device being used to generate diagnostic output. This
1775
may include, for a UART, setting baud rate, and stop, parity and
1776
character bits. For other devices it may include initializing a
1777
controller or establishing contact with a remote device.
1778
1779
 
1780
1781
HAL_DIAG_WRITE_CHAR(c) writes
1782
the character supplied to the diagnostic output device.
1783
1784
 
1785
1786
HAL_DIAG_READ_CHAR(c) reads a character from the
1787
diagnostic device into the supplied variable. This is not supported
1788
for all diagnostic devices.
1789
1790
 
1791
1792
These macros are defined in the header file
1793
cyg/hal/hal_diag.h. This file is usually supplied
1794
by the variant or platform HAL, depending on where the IO device being
1795
used is located. For example for on-chip UARTs it would be in the
1796
variant HAL, but for a board-level LCD panel it would be in the
1797
platform HAL.
1798
1799
 
1800
1801
 
1802
1803
1804
 
1805
1806
SMP Support
1807
 
1808
1809
eCos contains support for limited Symmetric Multi-Processing
1810
(SMP). This is only available on selected architectures and platforms.
1811
1812
 
1813
1814
Target Hardware Limitations
1815
 
1816
1817
To allow a reasonable implementation of SMP, and to reduce the
1818
disruption to the existing source base, a number of assumptions have
1819
been made about the features of the target hardware.
1820
1821
 
1822
1823
1824
1825
Modest multiprocessing. The typical number of CPUs supported is two
1826
to four, with an upper limit around eight. While there are no
1827
inherent limits in the code, hardware and algorithmic limitations
1828
will probably become significant beyond this point.
1829
1830
1831
 
1832
1833
1834
SMP synchronization support. The hardware must supply a mechanism to
1835
allow software on two CPUs to synchronize. This is normally provided
1836
as part of the instruction set in the form of test-and-set,
1837
compare-and-swap or load-link/store-conditional instructions. An
1838
alternative approach is the provision of hardware semaphore
1839
registers which can be used to serialize implementations of these
1840
operations. Whatever hardware facilities are available, they are
1841
used in eCos to implement spinlocks.
1842
1843
1844
 
1845
1846
1847
Coherent caches. It is assumed that no extra effort will be required
1848
to access shared memory from any processor. This means that either
1849
there are no caches, they are shared by all processors, or are
1850
maintained in a coherent state by the hardware. It would be too
1851
disruptive to the eCos sources if every memory access had to be
1852
bracketed by cache load/flush operations. Any hardware that requires
1853
this is not supported.
1854
1855
1856
 
1857
1858
1859
Uniform addressing. It is assumed that all memory that is
1860
shared between CPUs is addressed at the same location from all
1861
CPUs. Like non-coherent caches, dealing with CPU-specific address
1862
translation is considered too disruptive to the eCos source
1863
base. This does not, however, preclude systems with non-uniform
1864
access costs for different CPUs.
1865
1866
1867
 
1868
1869
1870
Uniform device addressing. As with access to memory, it is assumed
1871
that all devices are equally accessible to all CPUs. Since device
1872
access is often made from thread contexts, it is not possible to
1873
restrict access to device control registers to certain CPUs, since
1874
there is currently no support for binding or migrating threads to CPUs.
1875
1876
1877
 
1878
1879
1880
Interrupt routing. The target hardware must have an interrupt
1881
controller that can route interrupts to specific CPUs. It is
1882
acceptable for all interrupts to be delivered to just one CPU, or
1883
for some interrupts to be bound to specific CPUs, or for some
1884
interrupts to be local to each CPU. At present dynamic routing,
1885
where a different CPU may be chosen each time an interrupt is
1886
delivered, is not supported. ECos cannot support hardware where all
1887
interrupts are delivered to all CPUs simultaneously with the
1888
expectation that software will resolve any conflicts.
1889
1890
1891
 
1892
1893
1894
Inter-CPU interrupts. A mechanism to allow one CPU to interrupt
1895
another is needed. This is necessary so that events on one CPU can
1896
cause rescheduling on other CPUs.
1897
1898
1899
 
1900
1901
1902
CPU Identifiers. Code running on a CPU must be able to determine
1903
which CPU it is running on. The CPU Id is usually provided either in
1904
a CPU status register, or in a register associated with the
1905
inter-CPU interrupt delivery subsystem. ECos expects CPU Ids to be
1906
small positive integers, although alternative representations, such
1907
as bitmaps, can be converted relatively easily. Complex mechanisms
1908
for getting the CPU Id cannot be supported. Getting the CPU Id must
1909
be a cheap operation, since it is done often, and in performance
1910
critical places such as interrupt handlers and the scheduler.
1911
1912
1913
1914
 
1915
1916
 
1917
1918
HAL Support
1919
 
1920
1921
SMP support in any platform depends on the HAL supplying the
1922
appropriate operations. All HAL SMP support is defined in the
1923
cyg/hal/hal_smp.h header. Variant and platform
1924
specific definitions will be in cyg/hal/var_smp.h
1925
and cyg/hal/plf_smp.h respectively. These files
1926
are include automatically by this header, so need not be included
1927
explicitly.
1928
1929
 
1930
1931
SMP support falls into a number of functional groups.
1932
1933
 
1934
1935
CPU Control
1936
 
1937
1938
This group consists of descriptive and control macros for managing the
1939
CPUs in an SMP system.
1940
1941
 
1942
1943
1944
HAL_SMP_CPU_TYPE
1945
1946
1947
A type that can contain a CPU id. A CPU id is
1948
usually a small integer that is used to index
1949
arrays of variables that are managed on an
1950
per-CPU basis.
1951
1952
1953
1954
 
1955
1956
HAL_SMP_CPU_MAX
1957
1958
1959
The maximum number of CPUs that can be
1960
supported. This is used to provide the size of
1961
any arrays that have an element per CPU.
1962
1963
1964
1965
 
1966
1967
HAL_SMP_CPU_COUNT()
1968
1969
1970
Returns the number of CPUs currently
1971
operational. This may differ from
1972
HAL_SMP_CPU_MAX depending on the runtime
1973
environment.
1974
1975
1976
1977
 
1978
1979
HAL_SMP_CPU_THIS()
1980
1981
1982
Returns the CPU id of the current CPU.
1983
1984
1985
1986
 
1987
1988
HAL_SMP_CPU_NONE
1989
1990
1991
A value that does not match any real CPU
1992
id. This is uses where a CPU type variable
1993
must be set to a null value.
1994
1995
1996
1997
 
1998
1999
HAL_SMP_CPU_START( cpu )
2000
2001
2002
Starts the given CPU executing at a defined
2003
HAL entry point. After performing any HAL
2004
level initialization, the CPU calls up into
2005
the kernel at cyg_kernel_cpu_startup().
2006
2007
2008
2009
 
2010
2011
HAL_SMP_CPU_RESCHEDULE_INTERRUPT( cpu, wait )
2012
2013
2014
Sends the CPU a reschedule interrupt, and if
2015
wait is non-zero, waits for an
2016
acknowledgment. The interrupted CPU should call
2017
cyg_scheduler_set_need_reschedule() in its DSR to
2018
cause the reschedule to occur.
2019
2020
2021
2022
 
2023
2024
HAL_SMP_CPU_TIMESLICE_INTERRUPT( cpu, wait )
2025
2026
2027
Sends the CPU a timeslice interrupt, and if
2028
wait is non-zero, waits for an
2029
acknowledgment. The interrupted CPU should call
2030
cyg_scheduler_timeslice_cpu() to cause the
2031
timeslice event to be processed.
2032
2033
2034
2035
2036
2037
 
2038
 
2039
2040
Test-and-set Support
2041
 
2042
2043
Test-and-set is the foundation of the SMP synchronization
2044
mechanisms.
2045
2046
 
2047
2048
2049
HAL_TAS_TYPE
2050
2051
2052
The type for all test-and-set variables. The
2053
test-and-set macros only support operations on
2054
a single bit (usually the least significant
2055
bit) of this location. This allows for maximum
2056
flexibility in the implementation.
2057
2058
2059
2060
 
2061
2062
HAL_TAS_SET( tas, oldb )
2063
2064
2065
Performs a test and set operation on the
2066
location tas. oldb will contain true if
2067
the location was already set, and false if
2068
it was clear.
2069
2070
2071
2072
 
2073
2074
HAL_TAS_CLEAR( tas, oldb )
2075
2076
2077
Performs a test and clear operation on the
2078
location tas. oldb will contain true if
2079
the location was already set, and false if
2080
it was clear.
2081
2082
2083
2084
2085
2086
2087
 
2088
Spinlocks
2089
 
2090
2091
Spinlocks provide inter-CPU locking. Normally they will be implemented
2092
on top of the test-and-set mechanism above, but may also be
2093
implemented by other means if, for example, the hardware has more
2094
direct support for spinlocks.
2095
2096
 
2097
2098
2099
HAL_SPINLOCK_TYPE
2100
2101
2102
The type for all spinlock variables.
2103
2104
2105
2106
 
2107
2108
HAL_SPINLOCK_INIT_CLEAR
2109
2110
2111
A value that may be assigned to a spinlock
2112
variable to initialize it to clear.
2113
2114
2115
2116
 
2117
2118
HAL_SPINLOCK_INIT_SET
2119
2120
2121
A value that may be assigned to a spinlock
2122
variable to initialize it to set.
2123
2124
2125
2126
 
2127
2128
HAL_SPINLOCK_SPIN( lock )
2129
2130
2131
The caller spins in a busy loop waiting for
2132
the lock to become clear. It then sets it and
2133
continues. This is all handled atomically, so
2134
that there are no race conditions between CPUs.
2135
2136
2137
2138
 
2139
2140
HAL_SPINLOCK_CLEAR( lock )
2141
2142
2143
The caller clears the lock. One of any waiting
2144
spinners will then be able to proceed.
2145
2146
2147
2148
 
2149
2150
HAL_SPINLOCK_TRY( lock, val )
2151
2152
2153
Attempts to set the lock. The value put in
2154
val will be true if the lock was
2155
claimed successfully, and false if it was
2156
not.
2157
2158
2159
2160
 
2161
2162
HAL_SPINLOCK_TEST( lock, val )
2163
2164
2165
Tests the current value of the lock. The value
2166
put in val will be true if the lock is
2167
claimed and false of it is clear.
2168
2169
2170
2171
2172
2173
2174
 
2175
Scheduler Lock
2176
 
2177
2178
The scheduler lock is the main protection for all kernel data
2179
structures. By default the kernel implements the scheduler lock itself
2180
using a spinlock. However, if spinlocks cannot be supported by the
2181
hardware, or there is a more efficient implementation available, the
2182
HAL may provide macros to implement the scheduler lock.
2183
2184
 
2185
2186
2187
HAL_SMP_SCHEDLOCK_DATA_TYPE
2188
2189
2190
A data type, possibly a structure, that
2191
contains any data items needed by the
2192
scheduler lock implementation. A variable of
2193
this type will be instantiated as a static
2194
member of the Cyg_Scheduler_SchedLock class
2195
and passed to all the following macros.
2196
2197
2198
2199
 
2200
2201
HAL_SMP_SCHEDLOCK_INIT( lock, data )
2202
2203
2204
Initialize the scheduler lock. The lock
2205
argument is the scheduler lock counter and the
2206
data argument is a variable of
2207
HAL_SMP_SCHEDLOCK_DATA_TYPE type.
2208
2209
2210
2211
 
2212
2213
HAL_SMP_SCHEDLOCK_INC( lock, data )
2214
2215
2216
Increment the scheduler lock. The first
2217
increment of the lock from zero to one for any
2218
CPU may cause it to wait until the lock is
2219
zeroed by another CPU. Subsequent increments
2220
should be less expensive since this CPU
2221
already holds the lock.
2222
2223
2224
2225
 
2226
2227
HAL_SMP_SCHEDLOCK_ZERO( lock, data )
2228
2229
2230
Zero the scheduler lock. This operation will
2231
also clear the lock so that other CPUs may
2232
claim it.
2233
2234
2235
2236
 
2237
2238
HAL_SMP_SCHEDLOCK_SET( lock, data, new )
2239
2240
2241
Set the lock to a different value, in
2242
new. This is only called when the lock is
2243
already known to be owned by the current CPU. It is never called to
2244
zero the lock, or to increment it from zero.
2245
2246
2247
2248
 
2249
2250
2251
2252
 
2253
Interrupt Routing
2254
 
2255
2256
The routing of interrupts to different CPUs is supported by two new
2257
interfaces in hal_intr.h.
2258
2259
 
2260
2261
Once an interrupt has been routed to a new CPU, the existing vector
2262
masking and configuration operations should take account of the CPU
2263
routing. For example, if the operation is not invoked on the
2264
destination CPU itself, then the HAL may need to arrange to transfer
2265
the operation to the destination CPU for correct application.
2266
2267
 
2268
2269
2270
HAL_INTERRUPT_SET_CPU( vector, cpu )
2271
2272
2273
Route the interrupt for the given vector to
2274
the given cpu.
2275
2276
2277
2278
 
2279
2280
HAL_INTERRUPT_GET_CPU( vector, cpu )
2281
2282
2283
Set cpu to the id of the CPU to which this
2284
vector is routed.
2285
2286
2287
2288
2289
 
2290
2291
 
2292
2293
 
2294
 
2295
2296
 
2297
2298
 
2299
2300
 
2301
2302
2303
 
2304
2305
Exception Handling
2306
 
2307
2308
 
2309
2310
Most of the HAL consists of simple macros or functions that are
2311
called via the interfaces described in the previous section. These
2312
just perform whatever operation is required by accessing the hardware
2313
and then return. The exception to this is the handling of exceptions:
2314
either synchronous hardware traps or asynchronous device
2315
interrupts. Here control is passed first to the HAL, which then passed
2316
it on to eCos or the application. After eCos has finished with it,
2317
control is then passed back to the HAL for it to tidy up the CPU state
2318
and resume processing from the point at which the exception occurred.
2319
2320
 
2321
2322
The HAL exceptions handling code is usually found in the file
2323
vectors.S in the architecture HAL.  Since the
2324
reset entry point is usually implemented as one of these it also deals
2325
with system startup.
2326
2327
 
2328
2329
The exact implementation of this code is under the control of the HAL
2330
implementer. So long as it interacts correctly with the interfaces
2331
defined previously it may take any form.  However, all current
2332
implementation follow the same pattern, and there should be a very
2333
good reason to break with this. The rest of this section describes
2334
these operate.
2335
2336
 
2337
2338
Exception handling normally deals with the following broad areas of
2339
functionality:
2340
2341
 
2342
2343
  
2344
    Startup and initialization.
2345
  
2346
 
2347
  
2348
    Hardware exception delivery.
2349
  
2350
 
2351
  
2352
    Default handling of synchronous exceptions.
2353
  
2354
 
2355
  
2356
    Default handling of asynchronous interrupts.
2357
  
2358
2359
 
2360
2361
2362
 
2363
2364
<!-- <index></index> --><!-- <xref> -->HAL Startup
2365
 
2366
2367
Execution normally begins at the reset vector with
2368
the machine in a minimal startup state. From here the HAL needs to get
2369
the machine running, set up the execution environment for the
2370
application, and finally invoke its entry point.
2371
2372
 
2373
2374
The following is a list of the jobs that need to be done in
2375
approximately the order in which they should be accomplished. Many
2376
of these will not be needed in some configurations.
2377
2378
 
2379
2380
  
2381
  
2382
  Initialize the hardware. This may involve initializing several
2383
  subsystems in both the architecture, variant and platform
2384
  HALs. These include:
2385
  
2386
    
2387
      
2388
        
2389
        Initialize various CPU status registers. Most importantly, the CPU
2390
        interrupt mask should be set to disable interrupts.
2391
        
2392
      
2393
 
2394
      
2395
        
2396
        Initialize the MMU, if it is used. On many platforms it is
2397
        only possible to control the cacheability of address ranges
2398
        via the MMU. Also, it may be necessary to remap RAM and device
2399
        registers to locations other than their defaults. However, for
2400
        simplicity, the mapping should be kept as close to one-to-one
2401
        physical-to-virtual as possible.
2402
        
2403
      
2404
 
2405
      
2406
        
2407
        Set up the memory controller to access RAM, ROM and I/O devices
2408
        correctly. Until this is done it may not be possible to access
2409
        RAM. If this is a ROMRAM startup then the program code can
2410
        now be copied to its RAM address and control transferred to it.
2411
        
2412
      
2413
 
2414
      
2415
        
2416
        Set up any bus bridges and support chips. Often access to
2417
        device registers needs to go through various bus bridges and
2418
        other intermediary devices. In many systems these are combined
2419
        with the memory controller, so it makes sense to set these up
2420
        together. This is particularly important if early diagnostic
2421
        output needs to go through one of these devices.
2422
        
2423
      
2424
 
2425
      
2426
        
2427
        Set up diagnostic mechanisms. If the platform includes an LED or
2428
        LCD output device, it often makes sense to output progress
2429
        indications on this during startup. This helps with diagnosing
2430
        hardware and software errors.
2431
        
2432
      
2433
 
2434
      
2435
        
2436
        Initialize floating point and other extensions such as SIMD
2437
        and multimedia engines. It is usually necessary to enable
2438
        these and maybe initialize control and exception registers for
2439
        these extensions.
2440
        
2441
      
2442
 
2443
 
2444
      
2445
        
2446
        Initialize interrupt controller. At the very least, it should
2447
        be configured to mask all interrupts. It may also be necessary
2448
        to set up the mapping from the interrupt controller's vector
2449
        number space to the CPU's exception number space. Similar
2450
        mappings may need to be set up between primary and secondary
2451
        interrupt controllers.
2452
        
2453
      
2454
 
2455
      
2456
        
2457
        Disable and initialize the caches. The caches should not
2458
        normally be enabled at this point, but it may be necessary to
2459
        clear or initialize them so that they can be enabled
2460
        later. Some architectures require that the caches be
2461
        explicitly reinitialized after a power-on reset.
2462
        
2463
      
2464
 
2465
 
2466
      
2467
        
2468
        Initialize the timer, clock etc. While the timer used for RTC
2469
        interrupts will be initialized later, it may be necessary to
2470
        set up the clocks that drive it here.
2471
        
2472
      
2473
 
2474
    
2475
    
2476
    The exact order in which these initializations is done is
2477
    architecture or variant specific. It is also often not necessary
2478
    to do anything at all for some of these options. These fragments
2479
    of code should concentrate on getting the target up and running so
2480
    that C function calls can be made and code can be run. More
2481
    complex initializations that cannot be done in assembly code may
2482
    be postponed until calls to
2483
    hal_variant_init() or
2484
    hal_platform_init() are made.
2485
    
2486
 
2487
    
2488
    Not all of these initializations need to be done for all startup
2489
    types. In particular, RAM startups can reasonably assume that the
2490
    ROM monitor or loader has already done most of this work.
2491
    
2492
 
2493
  
2494
 
2495
  
2496
    
2497
    Set up the stack pointer, this allows subsequent initialization
2498
    code to make proper procedure calls. Usually the interrupt stack
2499
    is used for this purpose since it is available, large enough, and
2500
    will be reused for other purposes later.
2501
    
2502
  
2503
 
2504
  
2505
    
2506
    Initialize any global pointer register needed for access to
2507
    globally defined variables. This allows subsequent initialization
2508
    code to access global variables.
2509
    
2510
  
2511
 
2512
  
2513
    
2514
    If the system is starting from ROM, copy the ROM template of the
2515
    .data section out to its correct position in
2516
    RAM. ().
2517
    
2518
  
2519
 
2520
  
2521
    
2522
    Zero the .bss section.
2523
    
2524
  
2525
 
2526
  
2527
    
2528
    Create a suitable C call stack frame. This may involve making
2529
    stack space for call frames, and arguments, and initializing the
2530
    back pointers to halt a GDB backtrace operation.
2531
    
2532
  
2533
 
2534
  
2535
    
2536
    Call hal_variant_init() and
2537
    hal_platform_init(). These will perform any
2538
    additional initialization needed by the variant and platform. This
2539
    typically includes further initialization of the interrupt
2540
    controller, PCI bus bridges, basic IO devices and enabling the
2541
    caches.
2542
    
2543
  
2544
 
2545
  
2546
    
2547
    Call cyg_hal_invoke_constructors() to run any
2548
    static constructors.
2549
    
2550
  
2551
 
2552
  
2553
    
2554
    Call cyg_start(). If
2555
    cyg_start() returns, drop into an infinite
2556
    loop.
2557
    
2558
  
2559
2560
 
2561
2562
 
2563
2564
2565
 
2566
2567
Vectors and VSRs
2568
 
2569
2570
The CPU delivers all  exceptions, whether
2571
synchronous faults or asynchronous interrupts, to a set of hardware
2572
defined vectors. Depending on the architecture, these may be
2573
implemented in a number of different ways. Examples of existing
2574
mechanisms are:
2575
2576
 
2577
2578
  
2579
    PowerPC
2580
    
2581
      
2582
      Exceptions are vectored to locations 256 bytes apart starting at
2583
      either zero or 0xFFF00000. There are 16 such
2584
      vectors defined by the basic architecture and extra vectors may
2585
      be defined by specific variants. One of the base vectors is for
2586
      all external interrupts, and another is for the architecture
2587
      defined timer.
2588
      
2589
    
2590
  
2591
 
2592
  
2593
    MIPS
2594
    
2595
      
2596
      Most exceptions and all interrupts are vectored to a single
2597
      address at either 0x80000000 or
2598
      0xBFC00180. Software is responsible for
2599
      reading the exception code from the CPU cause
2600
      register to discover its true source. Some TLB and debug
2601
      exceptions are delivered to different vector addresses, but
2602
      these are not used currently by eCos. One of the exception codes
2603
      in the cause register indicates an external
2604
      interrupt. Additional bits in the cause
2605
      register provide a first-level decode for the interrupt source,
2606
      one of which represents an architecture defined timer.
2607
      
2608
    
2609
  
2610
 
2611
  
2612
    IA32
2613
    
2614
      
2615
      Exceptions are delivered via an Interrupt Descriptor Table (IDT)
2616
      which is essentially an indirection table indexed by exception
2617
      number. The IDT may be placed anywhere in memory. In PC hardware
2618
      the standard interrupt controller can be programmed to deliver
2619
      the external interrupts to a block of 16 vectors at any offset
2620
      in the IDT. There is no hardware supplied mechanism for
2621
      determining the vector taken, other than from the address jumped
2622
      to.
2623
      
2624
    
2625
  
2626
 
2627
  
2628
    ARM
2629
    
2630
      
2631
      All exceptions, including the FIQ and IRQ interrupts, are
2632
      vectored to locations four bytes apart starting at zero. There
2633
      is only room for one instruction here, which must immediately
2634
      jump out to handling code higher in memory. Interrupt sources
2635
      have to be decoded entirely from the interrupt controller.
2636
      
2637
    
2638
  
2639
2640
 
2641
 
2642
2643
With such a wide variety of hardware approaches, it is not possible to
2644
provide a generic mechanism for the substitution of exception vectors
2645
directly. Therefore, eCos translates all of these mechanisms in to a
2646
common approach that can be used by portable code on all platforms.
2647
2648
 
2649
2650
The mechanism implemented is to attach to each hardware vector a short
2651
piece of trampoline code that makes an indirect jump via a table to
2652
the actual handler for the exception. This handler is called the
2653
Vector Service Routine (VSR) and the table is called the VSR table.
2654
2655
 
2656
2657
The trampoline code performs the absolute minimum processing necessary
2658
to identify the exception source, and jump to the VSR. The VSR is then
2659
responsible for saving the CPU state and taking the necessary actions
2660
to handle the exception or interrupt. The entry conditions for the VSR
2661
are as close to the raw hardware exception entry state as possible -
2662
although on some platforms the trampoline will have had to move or
2663
reorganize some registers to do its job.
2664
2665
 
2666
2667
To make this more concrete, consider how the trampoline code operates
2668
in each of the architectures described above:
2669
2670
 
2671
 
2672
2673
  
2674
    PowerPC
2675
    
2676
      
2677
      A separate trampoline is contained in each of the vector
2678
      locations. This code saves a few work registers away to the
2679
      special purposes registers available, loads the exception number
2680
      into a register and then uses that to index the VSR table and
2681
      jump to the VSR. The VSR is entered with some registers move to
2682
      the SPRs, and one of the data register containing the number of
2683
      the vector taken.
2684
      
2685
    
2686
  
2687
 
2688
  
2689
    MIPS
2690
    
2691
      
2692
      A single trampoline routine attached to the common vector reads
2693
      the exception code out of the cause register
2694
      and uses that value to index the VSR table and jump to the VSR.
2695
      The trampoline uses the two registers defined in the ABI for
2696
      kernel use to do this, one of these will contain the exception
2697
      vector number for the VSR.
2698
      
2699
    
2700
  
2701
 
2702
  
2703
    IA32
2704
    
2705
      
2706
      There is a separate 3 or 4 instruction trampoline pointed to by
2707
      each active IDT table entry. The trampoline for exceptions that
2708
      also have an error code pop it from the stack and put it into a
2709
      memory location. Trampolines for non-error-code exceptions just
2710
      zero the memory location. Then all trampolines push an
2711
      interrupt/exception number onto the stack, and take an indirect
2712
      jump through a precalculated offset in the VSR table. This is
2713
      all done without saving any registers, using memory-only
2714
      operations. The VSR is entered with the vector number pushed
2715
      onto the stack on top of the standard hardware saved state.
2716
      
2717
    
2718
  
2719
 
2720
  
2721
    ARM
2722
    
2723
      
2724
      The trampoline consists solely of the single instruction at the
2725
      exception entry point. This is an indirect jump via a location
2726
      32 bytes higher in memory. These locations, from
2727
      0x20 up, form the VSR table. Since each VSR
2728
      is entered in a different CPU mode
2729
      (SVC,UNDEF,ABORT,IRQ or FIQ) there has to be a
2730
      different VSR for each exception that knows how to save the CPU
2731
      state correctly.
2732
      
2733
    
2734
  
2735
2736
 
2737
2738
 
2739
2740
2741
 
2742
2743
<!-- <index></index> -->Default Synchronous Exception Handling
2744
 
2745
2746
Most synchronous exception VSR table entries will point to a default
2747
exception VSR which is responsible for handling all exceptions in a
2748
generic manner. The default VSR simply saves the CPU state, makes any
2749
adjustments to the CPU state that is necessary, and calls
2750
cyg_hal_exception_handler().
2751
2752
 
2753
2754
cyg_hal_exception_handler() needs to pass the
2755
exception on to some handling code.  There are two basic destinations:
2756
enter GDB or pass the exception up to eCos. Exactly which
2757
destination is taken depends on the configuration. When the GDB stubs are
2758
included then the exception is passed to them, otherwise it is passed
2759
to eCos.
2760
2761
 
2762
2763
If an eCos application has been loaded by RedBoot then the VSR table
2764
entries will all point into RedBoot's exception VSR, and will
2765
therefore enter GDB if an exception occurs. If the eCos application
2766
wants to handle an exception itself, it needs to replace the the VSR
2767
table entry with one pointing to its own VSR. It can do this with the
2768
HAL_VSR_SET_TO_ECOS_HANDLER() macro.
2769
2770
 
2771
2772
 
2773
2774
2775
 
2776
2777
<!-- <index></index> -->Default Interrupt Handling
2778
 
2779
2780
Most asynchronous external interrupt vectors will point to a default
2781
interrupt VSR which decodes the actual interrupt being delivered from
2782
the interrupt controller and invokes the appropriate ISR.
2783
2784
 
2785
2786
The default interrupt VSR has a number of responsibilities if it is
2787
going to interact with the Kernel cleanly and allow interrupts to
2788
cause thread preemption.
2789
2790
 
2791
2792
To support this VSR an ISR vector table is needed. For each valid
2793
vector three pointers need to be stored: the ISR, its data pointer and
2794
an opaque (to the HAL) interrupt object pointer needed by the
2795
kernel. It is implementation defined whether these are stored in a
2796
single table of triples, or in three separate tables.
2797
2798
 
2799
2800
The VSR follows the following approximate plan:
2801
2802
 
2803
2804
  
2805
    
2806
    Save the CPU state. In non-debug configurations, it may be
2807
    possible to get away with saving less than the entire machine
2808
    state. The option
2809
    CYGDBG_HAL_COMMON_INTERRUPTS_SAVE_MINIMUM_CONTEXT
2810
    is supported in some targets to do this.
2811
    
2812
  
2813
 
2814
  
2815
    
2816
    Increment the kernel scheduler lock. This is a static member of
2817
    the Cyg_Scheduler class, however it has also been aliased to
2818
    cyg_scheduler_sched_lock so that it can be
2819
    accessed from assembly code.
2820
    
2821
  
2822
 
2823
  
2824
    
2825
    (Optional) Switch to an interrupt stack if not already running on
2826
    it. This allows nested interrupts to be delivered without needing
2827
    every thread to have a stack large enough to take the maximum
2828
    possible nesting. It is implementation defined how to detect
2829
    whether this is a nested interrupt but there are two basic
2830
    techniques. The first is to inspect the stack pointer and switch
2831
    only if it is not currently within the interrupt stack range; the
2832
    second is to maintain a counter of the interrupt nesting level and
2833
    switch only if it is zero. The option
2834
    CYGIMP_HAL_COMMON_INTERRUPTS_USE_INTERRUPT_STACK
2835
    controls whether this happens.
2836
    
2837
  
2838
 
2839
  
2840
    
2841
    Decode the actual external interrupt being delivered from
2842
    the interrupt controller. This will yield the ISR vector
2843
    number. The code to do this usually needs to come from the
2844
    variant or platform HAL, so is usually present in the form of a
2845
    macro or procedure callout.
2846
    
2847
  
2848
 
2849
  
2850
    
2851
    (Optional) Re-enable interrupts to permit nesting. At this point
2852
    we can potentially allow higher priority interrupts to occur. It
2853
    depends on the interrupt architecture of the CPU and platform
2854
    whether more interrupts will occur at this point, or whether they
2855
    will only be delivered after the current interrupt has been
2856
    acknowledged (by a call to
2857
    HAL_INTERRUPT_ACKNOWLEDGE() in the ISR).
2858
    
2859
  
2860
 
2861
  
2862
    
2863
    Using the ISR vector number as an index, retrieve the
2864
    ISR pointer and its data pointer from the ISR vector table.
2865
    
2866
  
2867
 
2868
  
2869
    
2870
    Construct a C call stack frame. This may involve making stack
2871
    space for call frames, and arguments, and initializing the back
2872
    pointers to halt a GDB backtrace operation.
2873
    
2874
  
2875
 
2876
  
2877
    
2878
    Call the ISR, passing the vector number and data pointer.  The
2879
    vector number and a pointer to the saved state should be preserved
2880
    across this call, preferably by storing them in registers that are
2881
    defined to be callee-saved by the calling conventions.
2882
    
2883
  
2884
 
2885
  
2886
    
2887
    If this is an un-nested interrupt and a separate interrupt
2888
    stack is being used, switch back to the interrupted thread's
2889
    own stack.
2890
    
2891
  
2892
 
2893
  
2894
    
2895
    Use the saved ISR vector number to get the interrupt object
2896
    pointer from the ISR vector table.
2897
    
2898
  
2899
 
2900
  
2901
    
2902
    Call interrupt_end() passing it the return
2903
    value from the ISR, the interrupt object pointer and a pointer to
2904
    the saved CPU state. This function is implemented by the Kernel
2905
    and is responsible for finishing off the interrupt
2906
    handling. Specifically, it may post a DSR depending on the ISR
2907
    return value, and will decrement the scheduler lock. If the lock
2908
    is zeroed by this operation then any posted DSRs may be called and
2909
    may in turn result in a thread context switch.
2910
    
2911
  
2912
 
2913
  
2914
    
2915
    The return from interrupt_end() may occur
2916
    some time after the call. Many other threads may have executed in
2917
    the meantime. So here all we may do is restore the machine state
2918
    and resume execution of the interrupted thread. Depending on the
2919
    architecture, it may be necessary to disable interrupts again for
2920
    part of this.
2921
    
2922
  
2923
 
2924
2925
 
2926
2927
The detailed order of these steps may vary slightly depending on the
2928
architecture, in particular where interrupts are enabled and disabled.
2929
2930
 
2931
2932
 
2933
2934
 
2935
2936
 
2937
2938
2939
 
2940
&hal-common-porting-sgml;
2941
 
2942
2943
2944
 
2945
2946
<!-- <index></index> --><!-- <xref> -->Future developments
2947
 
2948
2949
The HAL is not complete, and will evolve and increase over
2950
time. Among the intended developments are:
2951
2952
2953
2954
Common macros for interpreting the contents of a saved
2955
machine context. These would allow portable code, such as debug
2956
stubs, to extract such values as the program counter and stack pointer
2957
from a state without having to interpret a HAL_SavedRegisters structure
2958
directly.
2959
2960
2961
Debugging support. Macros to set and clear hardware and
2962
software breakpoints. Access to other areas of machine state may
2963
also be supported.
2964
2965
2966
Static initialization support. The current HAL provides a
2967
dynamic interface to things like thread context initialization and ISR
2968
attachment. We also need to be able to define the system entirely
2969
statically so that it is ready to go on restart, without needing to
2970
run code. This will require extra macros to define these
2971
initializations.  Such support may have a consequential effect on the
2972
current HAL specification.
2973
2974
2975
CPU state control. Many CPUs have both kernel and user
2976
states. Although it is not intended to run any code in user state
2977
for the foreseeable future, it is possible that this may happen
2978
eventually. If this is the case, then some minor changes may be needed
2979
to the current HAL API to accommodate this. These should mostly
2980
be extensions, but minor changes in semantics may also be required.
2981
2982
2983
Physical memory management. Many embedded systems have
2984
multiple memory areas with varying properties such as base address,
2985
size, speed, bus width, cacheability and persistence. An API is
2986
needed to support the discovery of this information about the machine's
2987
physical memory map.
2988
2989
2990
Memory management control. Some embedded processors have
2991
a memory management unit. In some cases this must be enabled to
2992
allow the cache to be controlled, particularly if different regions
2993
of memory must have different caching properties. For some purposes,
2994
in some systems, it will be useful to manipulate the MMU settings
2995
dynamically.
2996
2997
2998
Power management. Macros to access and control any power
2999
management mechanisms available on the CPU implementation. These
3000
would provide a substrate for a more general power management system
3001
that also involved device drivers and other hardware components.
3002
3003
3004
Generic serial line macros. Most serial line devices operate
3005
in the same way, the only real differences being exactly which bits
3006
in which registers perform the standard functions. It should be
3007
possible to develop a set of HAL macros that provide basic serial
3008
line services such as baud rate setting, enabling interrupts, polling
3009
for transmit or receive ready, transmitting and receiving data etc.
3010
Given these it should be possible to create a generic serial line
3011
device driver that will allow rapid bootstrapping on any new platform.
3012
It may be possible to extend this mechanism to other device types.
3013
3014
3015
3016
 
3017
3018
 
3019

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.