| 1 |
package platform |
| 2 |
|
| 3 |
// Largely based on |
| 4 |
// https://github.com/moby/moby/blob/bc846d2e8fe5538220e0c31e9d0e8446f6fbc022/distribution/cpuinfo_unix.go |
| 5 |
// Copyright 2012-2017 Docker, Inc. |
| 6 |
// |
| 7 |
// https://github.com/containerd/containerd/blob/726dcaea50883e51b2ec6db13caff0e7936b711d/platforms/cpuinfo.go |
| 8 |
// Copyright The containerd Authors. |
| 9 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 |
// you may not use this file except in compliance with the License. |
| 11 |
// You may obtain a copy of the License at |
| 12 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
// Unless required by applicable law or agreed to in writing, software |
| 14 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 |
// See the License for the specific language governing permissions and |
| 17 |
// limitations under the License. |
| 18 |
|
| 19 |
import ( |
| 20 |
"bufio" |
| 21 |
"fmt" |
| 22 |
"os" |
| 23 |
"runtime" |
| 24 |
"slices" |
| 25 |
"strings" |
| 26 |
|
| 27 |
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1" |
| 28 |
"github.com/sirupsen/logrus" |
| 29 |
"go.podman.io/image/v5/types" |
| 30 |
) |
| 31 |
|
| 32 |
// For Linux, the kernel has already detected the ABI, ISA and Features. |
| 33 |
// So we don't need to access the ARM registers to detect platform information |
| 34 |
// by ourselves. We can just parse these information from /proc/cpuinfo |
| 35 |
func getCPUInfo(pattern string) (info string, err error) { |
| 36 |
if runtime.GOOS != "linux" { |
| 37 |
return "", fmt.Errorf("getCPUInfo for OS %s not implemented", runtime.GOOS) |
| 38 |
} |
| 39 |
|
| 40 |
cpuinfo, err := os.Open("/proc/cpuinfo") |
| 41 |
if err != nil { |
| 42 |
return "", err |
| 43 |
} |
| 44 |
defer cpuinfo.Close() |
| 45 |
|
| 46 |
// Start to Parse the Cpuinfo line by line. For SMP SoC, we parse |
| 47 |
// the first core is enough. |
| 48 |
scanner := bufio.NewScanner(cpuinfo) |
| 49 |
for scanner.Scan() { |
| 50 |
newline := scanner.Text() |
| 51 |
list := strings.Split(newline, ":") |
| 52 |
|
| 53 |
if len(list) > 1 && strings.EqualFold(strings.TrimSpace(list[0]), pattern) { |
| 54 |
return strings.TrimSpace(list[1]), nil |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// Check whether the scanner encountered errors |
| 59 |
err = scanner.Err() |
| 60 |
if err != nil { |
| 61 |
return "", err |
| 62 |
} |
| 63 |
|
| 64 |
return "", fmt.Errorf("getCPUInfo for pattern: %s not found", pattern) |
| 65 |
} |
| 66 |
|
| 67 |
func getCPUVariantDarwinWindows(arch string) string { |
| 68 |
// Darwin and Windows only support v7 for ARM32 and v8 for ARM64 and so we can use |
| 69 |
// runtime.GOARCH to determine the variants |
| 70 |
var variant string |
| 71 |
switch arch { |
| 72 |
case "arm64": |
| 73 |
variant = "v8" |
| 74 |
case "arm": |
| 75 |
variant = "v7" |
| 76 |
default: |
| 77 |
variant = "" |
| 78 |
} |
| 79 |
|
| 80 |
return variant |
| 81 |
} |
| 82 |
|
| 83 |
func getCPUVariantArm() string { |
| 84 |
variant, err := getCPUInfo("Cpu architecture") |
| 85 |
if err != nil { |
| 86 |
logrus.Errorf("Couldn't get cpu architecture: %v", err) |
| 87 |
return "" |
| 88 |
} |
| 89 |
|
| 90 |
switch strings.ToLower(variant) { |
| 91 |
case "8", "aarch64": |
| 92 |
variant = "v8" |
| 93 |
case "7m", "?(12)", "?(13)", "?(14)", "?(15)", "?(16)", "?(17)": |
| 94 |
variant = "v7" |
| 95 |
case "7": |
| 96 |
// handle RPi Zero variant mismatch due to wrong variant from kernel |
| 97 |
// https://github.com/containerd/containerd/pull/4530 |
| 98 |
// https://www.raspberrypi.org/forums/viewtopic.php?t=12614 |
| 99 |
// https://github.com/moby/moby/pull/36121#issuecomment-398328286 |
| 100 |
model, err := getCPUInfo("model name") |
| 101 |
if err != nil { |
| 102 |
logrus.Errorf("Couldn't get cpu model name, it may be the corner case where variant is 6: %v", err) |
| 103 |
return "" |
| 104 |
} |
| 105 |
// model name is NOT a value provided by the CPU; it is another outcome of Linux CPU detection, |
| 106 |
// https://github.com/torvalds/linux/blob/190bf7b14b0cf3df19c059061be032bd8994a597/arch/arm/mm/proc-v6.S#L178C35-L178C35 |
| 107 |
// (matching happens based on value + mask at https://github.com/torvalds/linux/blob/190bf7b14b0cf3df19c059061be032bd8994a597/arch/arm/mm/proc-v6.S#L273-L274 ) |
| 108 |
// ARM CPU ID starts with a “main” ID register https://developer.arm.com/documentation/ddi0406/cb/System-Level-Architecture/System-Control-Registers-in-a-VMSA-implementation/VMSA-System-control-registers-descriptions--in-register-order/MIDR--Main-ID-Register--VMSA?lang=en , |
| 109 |
// but the ARMv6/ARMv7 differences are not a single dimension, https://developer.arm.com/documentation/ddi0406/cb/System-Level-Architecture/The-CPUID-Identification-Scheme?lang=en . |
| 110 |
// The Linux "cpu architecture" is determined by a “memory model” feature. |
| 111 |
// |
| 112 |
// So, the "armv6-compatible" check basically checks for a "v6 or v7 CPU, but not one found listed as a known v7 one in the .proc.info.init tables of |
| 113 |
// https://github.com/torvalds/linux/blob/190bf7b14b0cf3df19c059061be032bd8994a597/arch/arm/mm/proc-v7.S . |
| 114 |
if strings.HasPrefix(strings.ToLower(model), "armv6-compatible") { |
| 115 |
logrus.Debugf("Detected corner case, setting cpu variant to v6") |
| 116 |
variant = "v6" |
| 117 |
} else { |
| 118 |
variant = "v7" |
| 119 |
} |
| 120 |
case "6", "6tej": |
| 121 |
variant = "v6" |
| 122 |
case "5", "5t", "5te", "5tej": |
| 123 |
variant = "v5" |
| 124 |
case "4", "4t": |
| 125 |
variant = "v4" |
| 126 |
case "3": |
| 127 |
variant = "v3" |
| 128 |
default: |
| 129 |
variant = "" |
| 130 |
} |
| 131 |
|
| 132 |
return variant |
| 133 |
} |
| 134 |
|
| 135 |
func getCPUVariant(os string, arch string) string { |
| 136 |
if os == "darwin" || os == "windows" { |
| 137 |
return getCPUVariantDarwinWindows(arch) |
| 138 |
} |
| 139 |
if arch == "arm" || arch == "arm64" { |
| 140 |
return getCPUVariantArm() |
| 141 |
} |
| 142 |
return "" |
| 143 |
} |
| 144 |
|
| 145 |
// compatibility contains, for a specified architecture, a list of known variants, in the |
| 146 |
// order from most capable (most restrictive) to least capable (most compatible). |
| 147 |
// Architectures that don’t have variants should not have an entry here. |
| 148 |
var compatibility = map[string][]string{ |
| 149 |
"arm": {"v8", "v7", "v6", "v5"}, |
| 150 |
"arm64": {"v8"}, |
| 151 |
} |
| 152 |
|
| 153 |
// WantedPlatforms returns all compatible platforms with the platform specifics possibly overridden by user, |
| 154 |
// the most compatible platform is first. |
| 155 |
// If some option (arch, os, variant) is not present, a value from current platform is detected. |
| 156 |
func WantedPlatforms(ctx *types.SystemContext) []imgspecv1.Platform { |
| 157 |
// Note that this does not use Platform.OSFeatures and Platform.OSVersion at all. |
| 158 |
// The fields are not specified by the OCI specification, as of version 1.1, usefully enough |
| 159 |
// to be interoperable, anyway. |
| 160 |
|
| 161 |
wantedArch := runtime.GOARCH |
| 162 |
wantedVariant := "" |
| 163 |
if ctx != nil && ctx.ArchitectureChoice != "" { |
| 164 |
wantedArch = ctx.ArchitectureChoice |
| 165 |
} else { |
| 166 |
// Only auto-detect the variant if we are using the default architecture. |
| 167 |
// If the user has specified the ArchitectureChoice, don't autodetect, even if |
| 168 |
// ctx.ArchitectureChoice == runtime.GOARCH, because we have no idea whether the runtime.GOARCH |
| 169 |
// value is relevant to the use case, and if we do autodetect a variant, |
| 170 |
// ctx.VariantChoice can't be used to override it back to "". |
| 171 |
wantedVariant = getCPUVariant(runtime.GOOS, runtime.GOARCH) |
| 172 |
} |
| 173 |
if ctx != nil && ctx.VariantChoice != "" { |
| 174 |
wantedVariant = ctx.VariantChoice |
| 175 |
} |
| 176 |
|
| 177 |
wantedOS := runtime.GOOS |
| 178 |
if ctx != nil && ctx.OSChoice != "" { |
| 179 |
wantedOS = ctx.OSChoice |
| 180 |
} |
| 181 |
|
| 182 |
var variants []string = nil |
| 183 |
if wantedVariant != "" { |
| 184 |
// If the user requested a specific variant, we'll walk down |
| 185 |
// the list from most to least compatible. |
| 186 |
if variantOrder := compatibility[wantedArch]; variantOrder != nil { |
| 187 |
if i := slices.Index(variantOrder, wantedVariant); i != -1 { |
| 188 |
variants = variantOrder[i:] |
| 189 |
} |
| 190 |
} |
| 191 |
if variants == nil { |
| 192 |
// user wants a variant which we know nothing about - not even compatibility |
| 193 |
variants = []string{wantedVariant} |
| 194 |
} |
| 195 |
// Make sure to have a candidate with an empty variant as well. |
| 196 |
variants = append(variants, "") |
| 197 |
} else { |
| 198 |
// Make sure to have a candidate with an empty variant as well. |
| 199 |
variants = append(variants, "") |
| 200 |
// If available add the entire compatibility matrix for the specific architecture. |
| 201 |
if possibleVariants, ok := compatibility[wantedArch]; ok { |
| 202 |
variants = append(variants, possibleVariants...) |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
res := make([]imgspecv1.Platform, 0, len(variants)) |
| 207 |
for _, v := range variants { |
| 208 |
res = append(res, imgspecv1.Platform{ |
| 209 |
OS: wantedOS, |
| 210 |
Architecture: wantedArch, |
| 211 |
Variant: v, |
| 212 |
}) |
| 213 |
} |
| 214 |
return res |
| 215 |
} |
| 216 |
|
| 217 |
// MatchesPlatform returns true if a platform descriptor from a multi-arch image matches |
| 218 |
// an item from the return value of WantedPlatforms. |
| 219 |
func MatchesPlatform(image imgspecv1.Platform, wanted imgspecv1.Platform) bool { |
| 220 |
return image.Architecture == wanted.Architecture && |
| 221 |
image.OS == wanted.OS && |
| 222 |
image.Variant == wanted.Variant |
| 223 |
} |