github.com/usbarmory/GoTEE@v0.0.0-20240405084336-c52770d9fcdb/monitor/handler.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 monitor 8 9 import ( 10 "crypto/rand" 11 "errors" 12 "fmt" 13 "log" 14 "time" 15 16 "github.com/usbarmory/GoTEE/syscall" 17 ) 18 19 // SecureHandler is the default handler for exceptions raised by a secure 20 // execution context to handle supported GoTEE system calls. 21 func SecureHandler(ctx *ExecCtx) (err error) { 22 switch num := ctx.A0(); num { 23 case syscall.SYS_EXIT: 24 ctx.Stop() 25 case syscall.SYS_WRITE: 26 print(string(ctx.A1())) 27 case syscall.SYS_NANOTIME: 28 ctx.Ret(time.Now().UnixNano()) 29 case syscall.SYS_GETRANDOM: 30 off, n, err := ctx.TransferRegion() 31 32 if err != nil { 33 return err 34 } 35 36 buf := make([]byte, n) 37 38 if _, err := rand.Read(buf); err != nil { 39 return errors.New("internal error") 40 } 41 42 ctx.Memory.Write(ctx.Memory.Start(), off, buf) 43 case syscall.SYS_RPC_REQ, syscall.SYS_RPC_RES: 44 if ctx.Server != nil { 45 err = ctx.rpc() 46 } 47 default: 48 err = fmt.Errorf("invalid syscall %d", num) 49 } 50 51 return 52 } 53 54 // NonSecureHandler is the default handler for exceptions raised by a 55 // non-secure execution context to handle supported GoTEE secure monitor calls. 56 func NonSecureHandler(ctx *ExecCtx) (err error) { 57 // to be overridden by application 58 log.Printf("NonSecureHandler: unimplemented") 59 return 60 }