Unverified Commit c2c61818 authored by Kroese's avatar Kroese Committed by GitHub
Browse files

feat: Improve CPU compatibility and performance (#425)

parent d03f7790
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
{
  "name": "macOS 14 (Sonoma)",
  "service": "macos",
  "containerEnv": {
    "VERSION": "14"
  },
  "forwardPorts": [8006],
  "portsAttributes": {
    "8006": {
      "label": "Web",
      "onAutoForward": "notify"
    }
  },
  "otherPortsAttributes": {
    "onAutoForward": "ignore"
  }, 
  "dockerComposeFile": "../codespaces.yml",
  "initializeCommand": "docker system prune --all --force"
}
+2 −2
Original line number Diff line number Diff line
{
  "name": "macOS 14 (Sonoma)",
  "name": "macOS 26 (Tahoe)",
  "service": "macos",
  "containerEnv": {
    "VERSION": "14"
    "VERSION": "26"
  },
  "forwardPorts": [8006],
  "portsAttributes": {
+2 −2
Original line number Diff line number Diff line
@@ -94,7 +94,7 @@ kubectl apply -f https://raw.githubusercontent.com/dockur/macos/refs/heads/maste

  ```yaml
  environment:
    VERSION: "15"
    VERSION: "13"
  ```

  Select from the values below:
@@ -108,7 +108,7 @@ kubectl apply -f https://raw.githubusercontent.com/dockur/macos/refs/heads/maste
  | `11`        | macOS 11       | Big Sur          |

> [!NOTE]
> Support for macOS 15 (Sequoia) is still in its infancy, as it does not allow you to sign in to your Apple Account yet. 
> Starting with macOS 15 (Sequoia), you cannot sign in to an Apple Account unless you install [VMHide.kext](https://github.com/Carnations-Botanica/VMHide) first.

### How do I change the storage location?

+0 −82
Original line number Diff line number Diff line
@@ -175,86 +175,4 @@ BOOT_DRIVE_ID="OpenCore"
DISK_OPTS+=" -device virtio-blk-pci,drive=${BOOT_DRIVE_ID},bus=pcie.0,addr=0x5,bootindex=$BOOT_INDEX"
DISK_OPTS+=" -drive file=$IMG,id=$BOOT_DRIVE_ID,format=raw,cache=unsafe,readonly=on,if=none"

CPU_VENDOR=$(lscpu | awk '/Vendor ID/{print $3}')
DEFAULT_FLAGS="vendor=GenuineIntel,vmx=off,vmware-cpuid-freq=on,-pdpe1gb"

if [[ "$CPU_VENDOR" == "AuthenticAMD" || "${KVM:-}" == [Nn]* ]]; then

  if [ -z "${CPU_MODEL:-}" ]; then

    case "${VERSION,,}" in
      "ventura" | "13"* )
        CPU_MODEL="Haswell-noTSX" ;;
      "monterey" | "12"* )
        CPU_MODEL="Haswell-noTSX" ;;
      "bigsur" | "big-sur" | "11"* )
        CPU_MODEL="Haswell-noTSX" ;;
      "catalina" | "10"* )
        CPU_MODEL="Haswell-noTSX" ;;
      *)
        CPU_MODEL="Skylake-Client-v4"
        DEFAULT_FLAGS+=",-spec-ctrl"
        ;;
    esac

  fi

  if [[ "${KVM:-}" == [Nn]* ]] || [[ "${ARCH,,}" != "amd64" ]] || [[ "$OSTYPE" =~ ^darwin ]]; then
    DEFAULT_FLAGS+=",-pcid,-tsc-deadline,-invpcid,-xsavec,-xsaves,+ssse3,+sse4.2,+popcnt,+avx,+avx2,+aes,+fma,+bmi1,+bmi2,+smep,+xsave,+xsaveopt,+xgetbv1,+movbe,+rdrand,check"
  else
    DEFAULT_FLAGS+=",+pcid,+ssse3,+sse4.2,+popcnt,+avx,+avx2,+aes,+fma,+bmi1,+bmi2,+smep,+xsave,+xsavec,+xsaves,+xsaveopt,+xgetbv1,+movbe,+rdrand,check"
  fi

fi

if [ -z "${CPU_FLAGS:-}" ]; then
  CPU_FLAGS="$DEFAULT_FLAGS"
else
  CPU_FLAGS="$DEFAULT_FLAGS,$CPU_FLAGS"
fi

SM_BIOS=""
CLOCKSOURCE="tsc"
[[ "${ARCH,,}" == "arm64" ]] && CLOCKSOURCE="arch_sys_counter"
CLOCK="/sys/devices/system/clocksource/clocksource0/current_clocksource"

if [ ! -f "$CLOCK" ]; then
  warn "file \"$CLOCK\" cannot be found?"
else
  result=$(<"$CLOCK")
  result="${result//[![:print:]]/}"
  case "${result,,}" in
    "${CLOCKSOURCE,,}" ) 
      if [[ "$CPU_VENDOR" == "GenuineIntel" && "$CPU_CORES" == "1" && "${KVM:-}" != [Nn]* ]]; then
        CPU_CORES="2"
      fi ;;
    "kvm-clock" ) warn "Nested KVM virtualization detected, this might cause issues running macOS!" ;;
    "hyperv_clocksource_tsc_page" ) info "Nested Hyper-V virtualization detected, this might cause issues running macOS!" ;;
    "hpet" ) warn "unsupported clock source detected: '$result'. Please set host clock source to '$CLOCKSOURCE', otherwise it will cause issues running macOS!" ;;
    *) warn "unexpected clock source detected: '$result'. Please set host clock source to '$CLOCKSOURCE', otherwise it will cause issues running macOS!" ;;
  esac
fi

case "$CPU_CORES" in
  "" | "0" | "3" ) CPU_CORES="2" ;;
  "5" ) CPU_CORES="4" ;;
  "9" ) CPU_CORES="8" ;;
esac

case "$CPU_CORES" in
  "1" | "2" | "4" | "8" ) SMP="$CPU_CORES,sockets=1,dies=1,cores=$CPU_CORES,threads=1" ;;
  "6" | "7" ) SMP="$CPU_CORES,sockets=3,dies=1,cores=2,threads=1" ;;
  "10" | "11" ) SMP="$CPU_CORES,sockets=5,dies=1,cores=2,threads=1" ;;
  "12" | "13" ) SMP="$CPU_CORES,sockets=3,dies=1,cores=4,threads=1" ;;
  "14" | "15" ) SMP="$CPU_CORES,sockets=7,dies=1,cores=2,threads=1" ;;
  "16" | "32" | "64" ) SMP="$CPU_CORES,sockets=1,dies=1,cores=$CPU_CORES,threads=1" ;;
  *)
    error "Invalid amount of CPU_CORES, value \"${CPU_CORES}\" is not a power of 2!" && exit 35
    ;;
esac

USB="nec-usb-xhci,id=xhci"
USB+=" -device usb-kbd,bus=xhci.0"
USB+=" -global nec-usb-xhci.msi=off"

return 0

src/cpu.sh

0 → 100644
+120 −0
Original line number Diff line number Diff line
#!/usr/bin/env bash
set -Eeuo pipefail

CPU_VENDOR=$(lscpu | awk '/Vendor ID/{print $3}')
DEFAULT_FLAGS="vendor=GenuineIntel,vmx=off,vmware-cpuid-freq=on,-pdpe1gb"

has_flag() {
  # Match a whitespace-delimited token in /proc/cpuinfo (works for flags containing '-' and avoids substring matches)
  awk -v f="$1" '
    $1 == "flags" {
      for (i = 1; i <= NF; i++) if ($i == f) exit 0
    }
    END { exit 1 }
  ' /proc/cpuinfo
}

if [[ "$CPU_VENDOR" == "AuthenticAMD" || "${KVM:-}" == [Nn]* ]]; then

  # Configuration for AMD processors

  if [ -z "${CPU_MODEL:-}" ]; then

    case "${VERSION,,}" in
      "10"* | "11"* | "12"* | "13"* | \
      "catalina" | "bigsur" | "big-sur" | "monterey" | "ventura" )
        CPU_MODEL="Haswell-noTSX"
        ;;
      *)
        CPU_MODEL="Skylake-Client-v4"
        if has_flag "spec-ctrl" && [[ "${KVM:-}" != [Nn]* ]]; then
          DEFAULT_FLAGS+=",+spec-ctrl"
        else
          DEFAULT_FLAGS+=",-spec-ctrl"
        fi
        ;;
    esac

  fi

  if [[ "${KVM:-}" == [Nn]* ]]; then
  
    DEFAULT_FLAGS+=",-pcid,-invpcid,-tsc-deadline,-xsavec,-xsaves"

  else

    for flag in pcid invpcid tsc-deadline xsavec xsaves; do
      if has_flag "$flag"; then
        DEFAULT_FLAGS+=",+$flag"
      else
        DEFAULT_FLAGS+=",-$flag"
      fi
    done

  fi

  DEFAULT_FLAGS+=",+ssse3,+sse4.2,+popcnt,+avx,+avx2,+aes,+fma,+bmi1,+bmi2,+smep,+xsave,+xsaveopt,+xgetbv1,+movbe,+rdrand,check"

else

  # Configuration for Intel processors
  
  if [ -z "${CPU_MODEL:-}" ]; then

    CPU_MODEL="Skylake-Client-v4"

  fi

fi

if [ -z "${CPU_FLAGS:-}" ]; then
  CPU_FLAGS="$DEFAULT_FLAGS"
else
  CPU_FLAGS="$DEFAULT_FLAGS,$CPU_FLAGS"
fi

SM_BIOS=""
CLOCKSOURCE="tsc"
[[ "${ARCH,,}" == "arm64" ]] && CLOCKSOURCE="arch_sys_counter"
CLOCK="/sys/devices/system/clocksource/clocksource0/current_clocksource"

if [ ! -f "$CLOCK" ]; then
  warn "file \"$CLOCK\" cannot be found?"
else
  result=$(<"$CLOCK")
  result="${result//[![:print:]]/}"
  case "${result,,}" in
    "${CLOCKSOURCE,,}" ) 
      if [[ "$CPU_VENDOR" == "GenuineIntel" && "$CPU_CORES" == "1" && "${KVM:-}" != [Nn]* ]]; then
        CPU_CORES="2"
      fi ;;
    "kvm-clock" ) warn "Nested KVM virtualization detected, this might cause issues running macOS!" ;;
    "hyperv_clocksource_tsc_page" ) info "Nested Hyper-V virtualization detected, this might cause issues running macOS!" ;;
    "hpet" ) warn "unsupported clock source detected: '$result'. Please set host clock source to '$CLOCKSOURCE', otherwise it will cause issues running macOS!" ;;
    *) warn "unexpected clock source detected: '$result'. Please set host clock source to '$CLOCKSOURCE', otherwise it will cause issues running macOS!" ;;
  esac
fi

case "$CPU_CORES" in
  "" | "0" | "3" ) CPU_CORES="2" ;;
  "5" ) CPU_CORES="4" ;;
  "9" ) CPU_CORES="8" ;;
esac

case "$CPU_CORES" in
  "1" | "2" | "4" | "8" ) SMP="$CPU_CORES,sockets=1,dies=1,cores=$CPU_CORES,threads=1" ;;
  "6" | "7" ) SMP="$CPU_CORES,sockets=3,dies=1,cores=2,threads=1" ;;
  "10" | "11" ) SMP="$CPU_CORES,sockets=5,dies=1,cores=2,threads=1" ;;
  "12" | "13" ) SMP="$CPU_CORES,sockets=3,dies=1,cores=4,threads=1" ;;
  "14" | "15" ) SMP="$CPU_CORES,sockets=7,dies=1,cores=2,threads=1" ;;
  "16" | "32" | "64" ) SMP="$CPU_CORES,sockets=1,dies=1,cores=$CPU_CORES,threads=1" ;;
  *)
    error "Invalid amount of CPU_CORES, value \"${CPU_CORES}\" is not a power of 2!" && exit 35
    ;;
esac

USB="nec-usb-xhci,id=xhci"
USB+=" -device usb-kbd,bus=xhci.0"
USB+=" -global nec-usb-xhci.msi=off"

return 0
Loading