github.com/usbarmory/tamago@v0.0.0-20240508072735-8612bbe1e454/arm/arm.go (about)

     1  // ARM processor support
     2  // https://github.com/usbarmory/tamago
     3  //
     4  // Copyright (c) WithSecure Corporation
     5  // https://foundry.withsecure.com
     6  //
     7  // Use of this source code is governed by the license
     8  // that can be found in the LICENSE file.
     9  
    10  // Package arm provides support for ARM architecture specific operations.
    11  //
    12  // The following architectures/cores are supported/tested:
    13  //   - ARMv7-A / Cortex-A7 (single-core)
    14  //
    15  // This package is only meant to be used with `GOOS=tamago GOARCH=arm` as
    16  // supported by the TamaGo framework for bare metal Go on ARM SoCs, see
    17  // https://github.com/usbarmory/tamago.
    18  package arm
    19  
    20  import "runtime"
    21  
    22  // ARM processor modes
    23  // (Table B1-1, ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition).
    24  const (
    25  	USR_MODE = 0b10000
    26  	FIQ_MODE = 0b10001
    27  	IRQ_MODE = 0b10010
    28  	SVC_MODE = 0b10011
    29  	MON_MODE = 0b10110
    30  	ABT_MODE = 0b10111
    31  	HYP_MODE = 0b11010
    32  	UND_MODE = 0b11011
    33  	SYS_MODE = 0b11111
    34  )
    35  
    36  // CPU instance
    37  type CPU struct {
    38  	// instruction sets
    39  	arm     bool
    40  	thumb   bool
    41  	jazelle bool
    42  	thumbee bool
    43  
    44  	// extensions
    45  	programmersModel bool
    46  	security         bool
    47  	mProfileModel    bool
    48  	virtualization   bool
    49  	genericTimer     bool
    50  
    51  	// timer multiplier
    52  	TimerMultiplier int64
    53  	// timer offset in nanoseconds
    54  	TimerOffset int64
    55  	// timer function
    56  	TimerFn func() int64
    57  
    58  	// GIC Distributor base address
    59  	gicd uint32
    60  	// GIC CPU interface base address
    61  	gicc uint32
    62  
    63  	// vector base address register
    64  	vbar uint32
    65  }
    66  
    67  // defined in arm.s
    68  func read_cpsr() uint32
    69  func halt()
    70  
    71  // Init performs initialization of an ARM core instance, the argument must be a
    72  // pointer to a 64 kB memory area which will be reserved for storing the
    73  // exception vector table, L1/L2 page tables and the exception stack
    74  // (see https://github.com/usbarmory/tamago/wiki/Internals#memory-layout).
    75  func (cpu *CPU) Init(vbar uint32) {
    76  	runtime.Exit = halt
    77  
    78  	// the application is allowed to override the reserved area
    79  	if vecTableStart != 0 {
    80  		vbar = vecTableStart
    81  	}
    82  
    83  	cpu.initFeatures()
    84  	cpu.initVectorTable(vbar)
    85  }
    86  
    87  // Mode returns the processor mode.
    88  func (cpu *CPU) Mode() int {
    89  	return int(read_cpsr() & 0x1f)
    90  }
    91  
    92  // ModeName returns the processor mode name.
    93  func ModeName(mode int) string {
    94  	switch mode {
    95  	case USR_MODE:
    96  		return "USR"
    97  	case FIQ_MODE:
    98  		return "FIQ"
    99  	case IRQ_MODE:
   100  		return "IRQ"
   101  	case SVC_MODE:
   102  		return "SVC"
   103  	case MON_MODE:
   104  		return "MON"
   105  	case ABT_MODE:
   106  		return "ABT"
   107  	case HYP_MODE:
   108  		return "HYP"
   109  	case UND_MODE:
   110  		return "UND"
   111  	case SYS_MODE:
   112  		return "SYS"
   113  	}
   114  
   115  	return "Unknown"
   116  }