github.com/usbarmory/GoTEE@v0.0.0-20240405084336-c52770d9fcdb/sbi/sbi.go (about)

     1  // Copyright (c) WithSecure Corporation
     2  // https://foundry.withsecure.com
     3  //
     4  // Use of this source code is governed by the license
     5  // that can be found in the LICENSE file.
     6  
     7  // Package sbi provides basic RISC-V Supervisor Binary Interface Specification
     8  // support for TamaGo unikernels launched in supervised mode through
     9  // monitor.Exec (see monitor package).
    10  //
    11  // This package is only meant to be used with `GOOS=tamago GOARCH=riscv64` as
    12  // supported by the TamaGo framework for bare metal Go on ARM SoCs, see
    13  // https://github.com/usbarmory/tamago.
    14  package sbi
    15  
    16  import (
    17  	"github.com/usbarmory/GoTEE/monitor"
    18  )
    19  
    20  const (
    21  	SBI_MAJOR = 1
    22  	SBI_MINOR = 0
    23  )
    24  
    25  // Supported SBI Extension IDs (EID)
    26  const (
    27  	EXT_BASE = 0x10
    28  )
    29  
    30  // Base Extension Function IDs (FIDs)
    31  const (
    32  	EXT_BASE_GET_SPEC_VERSION = iota
    33  	EXT_BASE_GET_IMP_ID
    34  	EXT_BASE_GET_IMP_VERSION
    35  	EXT_BASE_PROBE_EXT
    36  	EXT_BASE_GET_MVENDORID
    37  	EXT_BASE_GET_MARCHID
    38  	EXT_BASE_GET_MIMPID
    39  )
    40  
    41  // Standard SBI Errors
    42  const (
    43  	SBI_SUCCESS               = 0
    44  	SBI_ERR_FAILED            = -1
    45  	SBI_ERR_NOT_SUPPORTED     = -2
    46  	SBI_ERR_INVALID_PARAM     = -3
    47  	SBI_ERR_DENIED            = -4
    48  	SBI_ERR_INVALID_ADDRESS   = -5
    49  	SBI_ERR_ALREADY_AVAILABLE = -6
    50  	SBI_ERR_ALREADY_STARTED   = -7
    51  	SBI_ERR_ALREADY_STOPPED   = -8
    52  )
    53  
    54  type sbiret struct {
    55  	Error int64
    56  	Value int64
    57  }
    58  
    59  func baseHandler(ctx *monitor.ExecCtx) (ret sbiret) {
    60  	switch ctx.X16 {
    61  	case EXT_BASE_GET_SPEC_VERSION:
    62  		ret.Value = (SBI_MAJOR << 24) | SBI_MINOR
    63  	case EXT_BASE_GET_IMP_ID, EXT_BASE_GET_IMP_VERSION:
    64  		// report no supported EIDs or implementation details
    65  	case EXT_BASE_PROBE_EXT:
    66  		// report no support for other extensions
    67  	case EXT_BASE_GET_MVENDORID, EXT_BASE_GET_MARCHID, EXT_BASE_GET_MIMPID:
    68  		// zero is always a legal value for these CSRs
    69  	default:
    70  		ret.Error = SBI_ERR_NOT_SUPPORTED
    71  	}
    72  
    73  	return
    74  }
    75  
    76  // Handler implements basic support for RISC-V SBI calls raised by an execution
    77  // context, it provides minimal support for SBI probing by S-mode kernels. Only
    78  // the Base extension is implemented to report that no other SBI extensions are
    79  // available.
    80  func Handler(ctx *monitor.ExecCtx) (err error) {
    81  	var ret sbiret
    82  
    83  	switch ctx.X17 {
    84  	case EXT_BASE:
    85  		ret = baseHandler(ctx)
    86  	default:
    87  		ret.Error = SBI_ERR_NOT_SUPPORTED
    88  	}
    89  
    90  	ctx.X10 = uint64(ret.Error)
    91  	ctx.X11 = uint64(ret.Value)
    92  
    93  	return
    94  }