blob: a17504b7492e02e8d69a9c45e4011cbc4a1ac35f [file] [log] [blame]
Tomasz Jeznach2ee9f222021-04-12 13:41:43 -07001#!/bin/busybox sh
2# Copyright 2021 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# Bind PCI device to VFIO-PCI passthrough bus.
7vfio_pci() {
8 local sysfs=$1
9 local bdf=$(basename "${sysfs}")
10 local vendor_id=$(cat "${sysfs}/vendor")
11 local device_id=$(cat "${sysfs}/device")
12 local class=$(cat "${sysfs}/class")
13
14 # Filter by CLASS
15 case "${class}" in
16 0x060000)
17 return 1 # Ignore PCI Host Bridge.
18 ;;
19 0x060400)
20 return 1 # Ignore PCI-PCI Bridge.
21 ;;
22 esac
23
24 case "${bdf}" in
25 0000:00:1e.?)
26# Uncomment to keep hypervisor access to serial console.
27# return 1
28 ;;
29 esac
30
31 # Detach from current driver if any.
32 local driver="${sysfs}/driver"
33 if [[ -f "${driver}/unbind" ]]; then
34 echo "${bdf}" > "${driver}/unbind"
35 fi
36
37 # Attach pci device to VFIO-PCI bus driver.
38 echo "${vendor_id} ${device_id}" > /sys/bus/pci/drivers/vfio-pci/new_id
39}
40
Tomasz Jeznach3ee4a6e2021-05-12 12:03:06 -070041# Port I/O forwarding.
42get_platform_pmio() {
43 local pmio="${BOARD_PMIO:-128}"
44 echo "--no-legacy --direct-pmio /dev/port@${pmio}"
45}
46
Tomasz Jeznach2ee9f222021-04-12 13:41:43 -070047# Interrupt forwarding argument helper.
Tomasz Jeznach3ee4a6e2021-05-12 12:03:06 -070048get_platform_irqs() {
49 local irq
Tomasz Jeznach2ee9f222021-04-12 13:41:43 -070050 local args=""
Tomasz Jeznach3ee4a6e2021-05-12 12:03:06 -070051 local irqs="${BOARD_IRQS:-edge:1}"
Tomasz Jeznach2ee9f222021-04-12 13:41:43 -070052 for irq in ${irqs}; do
53 args="${args} --direct-${irq%:*}-irq ${irq#*:}"
54 done
55 echo "${args}"
56}
57
58# Get guest kernel command line options.
59get_cmdline() {
60 local cmd="$(cat /proc/cmdline)"
61
62 # PCI workaround
63 cmd="pci=lastbus=255 pci=nocrs pci=realloc=off ${cmd}"
64
65 # Uncomment to disable standard boot flow.
66 # cmd="${cmd/init=*root=/root=} init=/bin/bash"
67
68 # Uncomment for incorrect rootfs UUID workaround
69 # cmd="${cmd/root=*\ rootwait/root=\/dev\/nvme0n1p3}"
70
71 echo "${cmd}"
72}
73
74# Start primary guest VM.
75run() {
76 # Crosvm-direct command line.
77 local args="-s /run/crosvm.sock --cid 3"
78
79 # TODO(b/184871003): Enable user space IOAPIC implementation.
80 # args="${args} --split-irqchip"
81
82 # TODO(b/185150434): enable minijail sandboxing.
83 args="${args} --disable-sandbox"
84
85 # Use all available CPUs for primary VM.
86 local nproc=$(nproc)
87 local affinity=$(for n in $(seq 0 $((nproc - 1))); do echo -n "$n=$n:"; done)
88 args="${args} --cpus ${nproc} --cpu-affinity ${affinity%?}"
89
90 # Reserve 512MB of total system memory for hypervisor.
91 local mem_total=$(grep MemTotal /proc/meminfo)
92 local mem_kb=${mem_total//[^0-9]/}
93 local mem_vm=$((mem_kb/1024 - 512))
94 args="${args} --mem ${mem_vm}"
95
96 # DMI: Use host SMBIOS info
97 if [[ -d "/sys/firmware/dmi/tables" ]]; then
98 args="${args} --dmi /sys/firmware/dmi/tables"
99 fi
100
101 # ACPI: Collect all valid host tables.
102 for acpi in `find /sys/firmware/acpi/tables/ -type f`; do
103 # Filter by name
104 case "${acpi##*/}" in
105 DMAR | FACS)
106 ;;
107 *)
108 args="${args} --acpi-table ${acpi}"
109 ;;
110 esac
111 done
112
113 # VFIO: Disable functional reset (b/173632817).
114 echo 1 > /sys/module/vfio_pci/parameters/disable_function_reset
115 # VFIO: Disable power management.
116 echo 1 > /sys/module/vfio_pci/parameters/disable_idle_d3
117
118 # VFIO: Direct attach all usable PCI devices.
119 for dev in `find /sys/bus/pci/devices -type l | sort`; do
120 vfio_pci "${dev}"
121 if [[ $? -eq 0 ]]; then
122 args="${args} --vfio ${dev}"
123 fi
124 done
125
Tomasz Jeznach3ee4a6e2021-05-12 12:03:06 -0700126 # Platform IOs
127 args="${args} $(get_platform_pmio)"
Tomasz Jeznach2ee9f222021-04-12 13:41:43 -0700128
129 # Platform IRQs
Tomasz Jeznach3ee4a6e2021-05-12 12:03:06 -0700130 args="${args} $(get_platform_irqs)"
Tomasz Jeznach2ee9f222021-04-12 13:41:43 -0700131
132 exec /sbin/crosvm-direct run ${args} --params "$(get_cmdline)" /boot/vmlinuz
133}
134
Tomasz Jeznach3ee4a6e2021-05-12 12:03:06 -0700135if [[ -f "/etc/manatee.conf" ]]; then
136 source "/etc/manatee.conf"
137fi
138
Tomasz Jeznach2ee9f222021-04-12 13:41:43 -0700139run
140