-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution.go
More file actions
64 lines (55 loc) · 1.49 KB
/
execution.go
File metadata and controls
64 lines (55 loc) · 1.49 KB
1
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//go:build darwin && arm64
package hypervisor
/*
#cgo darwin LDFLAGS: -framework Hypervisor
#include <Hypervisor/hv_vcpu.h>
#include <Hypervisor/hv_vcpu_types.h>
// Helper to get ESR and FAR from the exit information
static hv_return_t go_hv_get_esr_far(hv_vcpu_t vcpu, uint64_t* esr, uint64_t* far) {
// For ARM64, we would get this from the exit structure, but for now
// try to get it from system registers
hv_return_t r1 = hv_vcpu_get_sys_reg(vcpu, HV_SYS_REG_ESR_EL1, esr);
hv_return_t r2 = hv_vcpu_get_sys_reg(vcpu, HV_SYS_REG_FAR_EL1, far);
return (r1 != HV_SUCCESS) ? r1 : r2;
}
*/
import "C"
import (
"fmt"
"time"
)
// Run executes the vCPU until it exits. Returns ExitInfo best-effort.
func (c *VCPU) Run() (ExitInfo, error) {
start := time.Now()
defer func() {
recordRun(time.Since(start))
}()
var info ExitInfo
if c == nil {
return info, fmt.Errorf("hv: VCPU is nil")
}
// Security: Lock to prevent use-after-free
c.closeMu.Lock()
defer c.closeMu.Unlock()
if c.closed {
return info, fmt.Errorf("hv: VCPU is closed")
}
ret := C.hv_vcpu_run(C.hv_vcpu_t(c.id))
if err := hvErr(ret); err != nil {
recordResourceError()
return info, fmt.Errorf("failed to run vCPU: %w", err)
}
var esr, far C.uint64_t
if C.go_hv_get_esr_far(C.hv_vcpu_t(c.id), &esr, &far) == C.HV_SUCCESS {
info.ESR = uint64(esr)
info.FAR = uint64(far)
if info.ESR != 0 {
info.Reason = ExitException
} else {
info.Reason = ExitUnknown
}
} else {
info.Reason = ExitUnknown
}
return info, nil
}