github.com/usbarmory/tamago@v0.0.0-20240508072735-8612bbe1e454/arm/tz.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
    11  
    12  import (
    13  	"github.com/usbarmory/tamago/internal/reg"
    14  )
    15  
    16  // defined in tz.s
    17  func read_scr() uint32
    18  func write_nsacr(scr uint32)
    19  
    20  // NonSecure returns whether the processor security mode is non-secure (e.g.
    21  // TrustZone Normal World.
    22  func (cpu *CPU) NonSecure() bool {
    23  	if !cpu.security {
    24  		return false
    25  	}
    26  
    27  	vecTable := cpu.vbar + 8*4
    28  	undefinedHandler := reg.Read(vecTable + UNDEFINED)
    29  
    30  	// NonSecure World cannot read the NS bit, the only way to infer it
    31  	// status is to trap the exception while attempting to read it.
    32  	reg.Write(vecTable+UNDEFINED, vector(nullHandler))
    33  	defer reg.Write(vecTable+UNDEFINED, undefinedHandler)
    34  
    35  	return read_scr()&1 == 1
    36  }
    37  
    38  // Secure returns whether the processor security mode is secure (e.g. TrustZone
    39  // Secure World).
    40  func (cpu *CPU) Secure() bool {
    41  	return !cpu.NonSecure()
    42  }
    43  
    44  // NonSecureAccessControl sets the NSACR register value, which defines the
    45  // Non-Secure access permissions to coprocessors.
    46  func (cpu *CPU) NonSecureAccessControl(nsacr uint32) {
    47  	if !cpu.security {
    48  		return
    49  	}
    50  
    51  	write_nsacr(nsacr)
    52  }