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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [hal/] [common/] [current/] [doc/] [hal.sgml] - Blame information for rev 856

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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