forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hypervisor_s390x.go
99 lines (83 loc) · 2.5 KB
/
hypervisor_s390x.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) IBM Corp. 2021
//
// SPDX-License-Identifier: Apache-2.0
//
package virtcontainers
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
const (
// This is valid in other architectures, but varcheck will complain
// when setting it in common code as it will be regarded unused
procKernelCmdline = "/proc/cmdline"
// Secure Execution
// https://www.kernel.org/doc/html/latest/virt/kvm/s390-pv.html
seCPUFacilityBit = 158
seCmdlineParam = "prot_virt"
)
var seCmdlineValues = []string{
"1", "on", "y", "yes",
}
// CPUFacilities retrieves active CPU facilities according to "Facility Indications", Principles of Operation.
// Takes cpuinfo path (such as /proc/cpuinfo), returns map of all active facility bits.
func CPUFacilities(cpuInfoPath string) (map[int]bool, error) {
facilitiesField := "facilities"
f, err := os.Open(cpuInfoPath)
if err != nil {
return map[int]bool{}, err
}
defer f.Close()
facilities := make(map[int]bool)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
// Expected format: ["facilities", ":", ...] or ["facilities:", ...]
fields := strings.Fields(scanner.Text())
if len(fields) < 2 {
continue
}
if !strings.HasPrefix(fields[0], facilitiesField) {
continue
}
start := 1
if fields[1] == ":" {
start = 2
}
for _, field := range fields[start:] {
bit, err := strconv.Atoi(field)
if err != nil {
return map[int]bool{}, err
}
facilities[bit] = true
}
return facilities, nil
}
if err := scanner.Err(); err != nil {
return map[int]bool{}, err
}
return map[int]bool{}, fmt.Errorf("Couldn't find %q from %q output", facilitiesField, cpuInfoPath)
}
// availableGuestProtection returns seProtection (Secure Execution) if available.
// Checks that Secure Execution is available (CPU facilities) and enabled (kernel command line).
func availableGuestProtection() (guestProtection, error) {
facilities, err := CPUFacilities(procCPUInfo)
if err != nil {
return noneProtection, err
}
if !facilities[seCPUFacilityBit] {
return noneProtection, fmt.Errorf("This CPU does not support Secure Execution")
}
seCmdlinePresent, err := CheckCmdline(procKernelCmdline, seCmdlineParam, seCmdlineValues)
if err != nil {
return noneProtection, err
}
if !seCmdlinePresent {
return noneProtection, fmt.Errorf("Protected Virtualization is not enabled on kernel command line! "+
"Need %s=%s (or %s) to enable Secure Execution",
seCmdlineParam, seCmdlineValues[0], strings.Join(seCmdlineValues[1:], ", "))
}
return seProtection, nil
}