mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 13:26:02 +00:00
Merge remote-tracking branch 'origin/main' into fix/steam-compat-pick-wins
This commit is contained in:
commit
a4abb797a7
140 files changed
+6314
-836
No files matched your search
@@ -227,7 +227,9 @@ if [ -z "${BL_INSIDE:-}" ]; then
|
||||
echo "== gamescope: games at ${nested}x${height} stretched to the ${width}x${height} output"
|
||||
fi
|
||||
fi
|
||||
if [ "$mode" = steam ]; then
|
||||
# Off unless Performance asks for it. Compositing the cursor here adds a second
|
||||
# plane on the phone's compositor; a controller-driven cursor is the reason to turn it on.
|
||||
if [ "$mode" = steam ] && [ "${BL_CURSOR_COMPOSITE:-0}" = 1 ]; then
|
||||
export gamescope_cursor_composite=2
|
||||
fi
|
||||
args=(--backend wayland --expose-wayland -f -W "$width" -H "$height" -w "$nested" -h "$height")
|
||||
@@ -403,6 +405,11 @@ case $mode in
|
||||
droiddeck-steam-x64 --sync-login || true
|
||||
fi
|
||||
steam_client=("$steam_root/steamrtarm64/steam")
|
||||
_bl_srt_shim=$HOME/.droiddeck-steamrt-shim
|
||||
if droiddeck-steamrt-shim "$steam_root/steamrtarm64" "$_bl_srt_shim"; then
|
||||
echo "== steam: Valve containerized the arm64 Steam client (it carries a Steam Runtime container); starting it through $_bl_srt_shim"
|
||||
steam_client=(env STEAM_RUNTIME_STEAMRT="$_bl_srt_shim" PATH="$_bl_srt_shim/bin:$PATH" "${steam_client[@]}")
|
||||
fi
|
||||
fi
|
||||
_bl_proton_root=$steam_root
|
||||
if [ "$steam_x64" = 1 ]; then
|
||||
@@ -526,6 +533,11 @@ case $mode in
|
||||
fi
|
||||
droiddeck-seed-redists "$steam_root" || true
|
||||
fi
|
||||
if [ -n "${BL_SHADER_PRECACHE:-}" ]; then
|
||||
droiddeck-steam-shaders "$BL_SHADER_PRECACHE" "$XDG_DATA_HOME/Steam" \
|
||||
"${BL_X64_HOME:-$HOME/.droiddeck-x64}/.local/share/Steam" &&
|
||||
echo "$BL_SHADER_PRECACHE" > "$HOME/.bl-shader-precache-applied" || true
|
||||
fi
|
||||
if [ "$steam_x64" = 0 ] && [ ! -f "$steam_root/steamapps/appmanifest_4427310.acf" ]; then
|
||||
step "asking the client to fetch the ARM64 Proton"
|
||||
set -- "$@" steam://install/4427310
|
||||
@@ -746,10 +758,13 @@ BUSCONF
|
||||
fi
|
||||
|
||||
# The UI bridge launches SteamVR, which needs the guest agent's library environment.
|
||||
_bl_compat_env=(env -u LD_LIBRARY_PATH -u STEAM_RUNTIME)
|
||||
_bl_compat_env=(env -u LD_LIBRARY_PATH -u STEAM_RUNTIME -u STEAM_STEAMRT_RECURSION_GUARD)
|
||||
[ "${LD_LIBRARY_PATH+x}" ] && _bl_compat_env+=("LD_LIBRARY_PATH=$LD_LIBRARY_PATH")
|
||||
[ "${STEAM_RUNTIME+x}" ] && _bl_compat_env+=("STEAM_RUNTIME=$STEAM_RUNTIME")
|
||||
[ "${STEAM_STEAMRT_RECURSION_GUARD+x}" ] && _bl_compat_env+=("STEAM_STEAMRT_RECURSION_GUARD=$STEAM_STEAMRT_RECURSION_GUARD")
|
||||
export STEAM_RUNTIME=1
|
||||
# A containerized client re-executes itself through pressure-vessel-wrap (bwrap) unless told it is already inside.
|
||||
export STEAM_STEAMRT_RECURSION_GUARD=1
|
||||
export LD_LIBRARY_PATH=$steam_root/steamrtarm64:$steam_root/steamrtarm64/panorama${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
|
||||
export SDL_VIDEO_X11_DGAMOUSE=0
|
||||
decky_loader=$HOME/homebrew/services/PluginLoader
|
||||
@@ -829,6 +844,9 @@ BUSCONF
|
||||
unset _stale _f
|
||||
fi
|
||||
steam_channel=steamdeck_publicbeta
|
||||
if [ "$steam_x64" = 0 ]; then
|
||||
steam_channel=$(droiddeck-steam-install --channel 2>/dev/null || echo steamdeck_publicbeta)
|
||||
fi
|
||||
_bl_lang=()
|
||||
[ -n "${BL_STEAM_LANGUAGE:-}" ] && _bl_lang=(-language "$BL_STEAM_LANGUAGE")
|
||||
steam_beta=$steam_root/package/beta
|
||||
|
||||
@@ -1,9 +1,26 @@
|
||||
#!/bin/bash
|
||||
set -eu
|
||||
cdn_list="https://client-update.steamstatic.com https://client-update.akamai.steamstatic.com https://client-update.fastly.steamstatic.com"
|
||||
steam_channel=steamdeck_publicbeta
|
||||
manifest=steam_client_${steam_channel}_linuxarm64
|
||||
beta_channel=steamdeck_publicbeta
|
||||
stable_channel=steamdeck_stable
|
||||
steam_root=${XDG_DATA_HOME:-$HOME/.local/share}/Steam
|
||||
channel_state=$steam_root/package/droiddeck-channel
|
||||
pinned=1
|
||||
wanted=${BL_STEAM_CHANNEL:-$(cat "$HOME/.config/droiddeck/steam-channel" 2>/dev/null || true)}
|
||||
case $wanted in
|
||||
stable|"$stable_channel") steam_channel=$stable_channel ;;
|
||||
beta|"$beta_channel") steam_channel=$beta_channel ;;
|
||||
*)
|
||||
pinned=
|
||||
steam_channel=$beta_channel
|
||||
[ "$(cat "$channel_state" 2>/dev/null)" = "$stable_channel" ] && steam_channel=$stable_channel
|
||||
;;
|
||||
esac
|
||||
if [ "${1:-}" = --channel ]; then
|
||||
echo "$steam_channel"
|
||||
exit 0
|
||||
fi
|
||||
manifest=steam_client_${steam_channel}_linuxarm64
|
||||
stamp=$steam_root/package/droiddeck-installed
|
||||
legacy_stamp=$steam_root/package/bannerlator-installed
|
||||
unconfirmed=$steam_root/package/droiddeck-unconfirmed
|
||||
@@ -12,11 +29,21 @@ if [ -f "$legacy_stamp" ] && [ ! -f "$stamp" ]; then
|
||||
mv -f "$legacy_stamp" "$stamp" || true
|
||||
fi
|
||||
repair=""
|
||||
fallback=""
|
||||
starts=0
|
||||
[ -f "$unconfirmed" ] && starts=$(grep -cx start "$unconfirmed" || true)
|
||||
if [ -e "$repair_request" ]; then
|
||||
repair="requested"
|
||||
elif [ -f "$unconfirmed" ] && ! grep -qx repaired "$unconfirmed" \
|
||||
&& [ "$(grep -cx start "$unconfirmed")" -ge 2 ]; then
|
||||
repair="the client was started $(grep -cx start "$unconfirmed") times without showing anything"
|
||||
elif [ "$starts" -ge 2 ] && ! grep -qx repaired "$unconfirmed"; then
|
||||
repair="the client was started $starts times without showing anything"
|
||||
elif [ "$starts" -ge 2 ] && [ -z "$pinned" ] && [ "$steam_channel" = "$beta_channel" ] && [ "${1:-}" != --sync ]; then
|
||||
repair="the repaired $beta_channel client was started $starts more times without showing anything"
|
||||
fallback=1
|
||||
fi
|
||||
if [ -n "$fallback" ]; then
|
||||
echo "== STEP $(date -u +%H:%M:%S) falling back to Valve's $stable_channel Steam client: $repair"
|
||||
steam_channel=$stable_channel
|
||||
manifest=steam_client_${steam_channel}_linuxarm64
|
||||
fi
|
||||
if [ -n "$repair" ]; then
|
||||
echo "== STEP $(date -u +%H:%M:%S) repairing the Steam client ($repair)"
|
||||
@@ -42,20 +69,42 @@ fi
|
||||
|
||||
work=$(mktemp -d)
|
||||
trap 'rm -rf "$work"' EXIT
|
||||
cdn=""
|
||||
for candidate in $cdn_list; do
|
||||
if curl -fsSL --connect-timeout 10 --max-time 30 --retry 2 --retry-connrefused --retry-delay 3 \
|
||||
"$candidate/$manifest" -o "$work/manifest"; then
|
||||
cdn=$candidate
|
||||
break
|
||||
fetch_manifest() {
|
||||
cdn=""
|
||||
for candidate in $cdn_list; do
|
||||
if curl -fsSL --connect-timeout 10 --max-time 30 --retry 2 --retry-connrefused --retry-delay 3 \
|
||||
"$candidate/$manifest" -o "$work/manifest"; then
|
||||
cdn=$candidate
|
||||
break
|
||||
fi
|
||||
done
|
||||
[ -n "$cdn" ] || { echo "droiddeck-steam-install: no Steam client CDN answered for $manifest" >&2; exit 1; }
|
||||
cdn_order=("$cdn")
|
||||
for candidate in $cdn_list; do
|
||||
[ "$candidate" = "$cdn" ] || cdn_order+=("$candidate")
|
||||
done
|
||||
version=$(awk -F'"' '$2 == "version" { print $4; exit }' "$work/manifest")
|
||||
awk -F'"' '
|
||||
/^\t"/ && NF >= 3 && $4 == "" { block = $2 }
|
||||
$2 == "file" { file[block] = $4 }
|
||||
$2 == "sha2" { sha[block] = $4 }
|
||||
END { for (b in file) if (b ~ /_all$/ || b ~ /_linuxarm64_linuxarm64$/) print b, file[b], sha[b] }
|
||||
' "$work/manifest" | sort > "$work/parts"
|
||||
[ -s "$work/parts" ] && return 0
|
||||
echo "droiddeck-steam-install: no client components in $manifest; its blocks are:" \
|
||||
"$(awk -F'"' '/^\t"/ && NF >= 3 && $4 == "" { printf "%s ", $2 }' "$work/manifest")" >&2
|
||||
return 1
|
||||
}
|
||||
if ! fetch_manifest; then
|
||||
if [ -n "$sync" ] || [ -n "$pinned" ] || [ "$steam_channel" = "$stable_channel" ]; then
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
[ -n "$cdn" ] || { echo "droiddeck-steam-install: no Steam client CDN answered for $manifest" >&2; exit 1; }
|
||||
cdn_order=("$cdn")
|
||||
for candidate in $cdn_list; do
|
||||
[ "$candidate" = "$cdn" ] || cdn_order+=("$candidate")
|
||||
done
|
||||
version=$(awk -F'"' '$2 == "version" { print $4; exit }' "$work/manifest")
|
||||
echo "== STEP $(date -u +%H:%M:%S) falling back to Valve's $stable_channel Steam client: $manifest has no arm64 client"
|
||||
steam_channel=$stable_channel
|
||||
manifest=steam_client_${steam_channel}_linuxarm64
|
||||
fallback=1
|
||||
fetch_manifest || exit 1
|
||||
fi
|
||||
if [ -n "$sync" ]; then
|
||||
installed=$(awk -F'"' '$2 == "version" { print $4; exit }' "$steam_root/package/$manifest.manifest" 2>/dev/null || true)
|
||||
if [ -x "$steam_root/steamrtarm64/steam" ] && [ "$installed" = "$version" ]; then
|
||||
@@ -64,13 +113,6 @@ if [ -n "$sync" ]; then
|
||||
fi
|
||||
echo "== STEP $(date -u +%H:%M:%S) updating the arm64 Steam client to $version (was ${installed:-none}) for the x86-64 client"
|
||||
fi
|
||||
awk -F'"' '
|
||||
/^\t"/ && NF >= 3 && $4 == "" { block = $2 }
|
||||
$2 == "file" { file[block] = $4 }
|
||||
$2 == "sha2" { sha[block] = $4 }
|
||||
END { for (b in file) if (b ~ /_all$/ || b ~ /_linuxarm64_linuxarm64$/) print b, file[b], sha[b] }
|
||||
' "$work/manifest" | sort > "$work/parts"
|
||||
[ -s "$work/parts" ] || { echo "droiddeck-steam-install: no client components in $manifest" >&2; exit 1; }
|
||||
total=$(grep -c . "$work/parts" 2>/dev/null || echo 0)
|
||||
|
||||
mkdir -p "$steam_root/package"
|
||||
@@ -117,6 +159,10 @@ ln -sfn "$steam_root/linuxarm64" "$HOME/.steam/sdkarm64"
|
||||
ln -sfn "$steam_root/steamrtarm64" "$HOME/.steam/binarm64"
|
||||
ln -sfn "$steam_root/linux32" "$HOME/.steam/sdk32"
|
||||
echo "$version" > "$stamp"
|
||||
if [ -n "$fallback" ]; then
|
||||
echo "$stable_channel" > "$channel_state"
|
||||
echo "== STEP $(date -u +%H:%M:%S) the Steam client now follows $stable_channel (beta in ~/.config/droiddeck/steam-channel returns to $beta_channel)"
|
||||
fi
|
||||
if [ -n "$repair" ]; then
|
||||
echo repaired > "$unconfirmed"
|
||||
rm -f "$repair_request"
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/python3
|
||||
"""Sets the Steam client's shader pre-caching (Settings > Downloads), before the client starts.
|
||||
|
||||
droiddeck-steam-shaders <0|1> <steam root>...
|
||||
|
||||
The client stores the toggle as InstallConfigStore/Software/Valve/Steam/ShaderCacheManager/
|
||||
DisableShaderCache in config/config.vdf: "1" when off, "0" when on. It keeps the setting in memory
|
||||
and writes config.vdf back on exit, so this runs only while the client is stopped. The launcher
|
||||
passes a value only after its own toggle changes; otherwise the client's toggle stays the user's."""
|
||||
from pathlib import Path
|
||||
import runpy
|
||||
import sys
|
||||
|
||||
|
||||
VDF = runpy.run_path(str(Path(__file__).with_name("droiddeck-steam-ui-scale")))
|
||||
PATH = ("Software", "Valve", "Steam", "ShaderCacheManager", "DisableShaderCache")
|
||||
|
||||
|
||||
def set_text(text, enabled):
|
||||
value = "0" if enabled else "1"
|
||||
updated, changed = VDF["seed_value"](text, PATH, value)
|
||||
if changed:
|
||||
return updated, True
|
||||
block = VDF["parse"](text).get("InstallConfigStore")
|
||||
for section in PATH[:-1]:
|
||||
block = block.get(section)
|
||||
current = block.get(PATH[-1])
|
||||
if isinstance(current, VDF["Block"]):
|
||||
raise ValueError("invalid DisableShaderCache setting")
|
||||
if current == value:
|
||||
return text, False
|
||||
at = block.offset(PATH[-1])
|
||||
end = VDF["TOKEN"].match(text, at).end()
|
||||
return text[:at] + f'"{value}"' + text[end:], True
|
||||
|
||||
|
||||
def apply(steam_root, enabled):
|
||||
config = Path(steam_root) / "config/config.vdf"
|
||||
text = config.read_text() if config.exists() else '"InstallConfigStore"\n{\n}\n'
|
||||
updated, changed = set_text(text, enabled)
|
||||
if changed:
|
||||
VDF["atomic_write"](config, updated)
|
||||
return changed
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 3 or sys.argv[1] not in ("0", "1"):
|
||||
print("usage: droiddeck-steam-shaders <0|1> <steam root>...", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
enabled = sys.argv[1] == "1"
|
||||
status = 0
|
||||
for root in sys.argv[2:]:
|
||||
if not Path(root).is_dir():
|
||||
continue
|
||||
try:
|
||||
if apply(root, enabled):
|
||||
print(f"== steam: shader pre-caching {'on' if enabled else 'off'} in {root}")
|
||||
except (OSError, ValueError, RecursionError) as error:
|
||||
print(f"== steam: left shader pre-caching unchanged in {root}: {error}", file=sys.stderr)
|
||||
status = 1
|
||||
sys.exit(status)
|
||||
@@ -26,9 +26,10 @@ TOKEN = re.compile(r'\s+|//[^\n]*|"(?:\\.|[^"\\])*"|[{}]')
|
||||
|
||||
|
||||
class Block:
|
||||
def __init__(self, pairs, end):
|
||||
def __init__(self, pairs, end, offsets=()):
|
||||
self.pairs = pairs
|
||||
self.end = end
|
||||
self.offsets = offsets
|
||||
|
||||
def get(self, name):
|
||||
matches = [value for key, value in self.pairs if key.lower() == name.lower()]
|
||||
@@ -36,6 +37,9 @@ class Block:
|
||||
raise ValueError("duplicate UI setting")
|
||||
return matches[0] if matches else None
|
||||
|
||||
def offset(self, name):
|
||||
return next(at for (key, _), at in zip(self.pairs, self.offsets) if key.lower() == name.lower())
|
||||
|
||||
|
||||
def parse(text):
|
||||
tokens = []
|
||||
@@ -51,17 +55,17 @@ def parse(text):
|
||||
raise ValueError("invalid config.vdf")
|
||||
|
||||
def block(index, nested=False):
|
||||
pairs = []
|
||||
pairs, offsets = [], []
|
||||
while index < len(tokens):
|
||||
token, offset = tokens[index]
|
||||
if token == '}':
|
||||
if not nested:
|
||||
raise ValueError("unexpected closing brace")
|
||||
return Block(pairs, offset), index + 1
|
||||
return Block(pairs, offset, offsets), index + 1
|
||||
if not token.startswith('"') or index + 1 == len(tokens):
|
||||
raise ValueError("missing config value")
|
||||
key = token[1:-1]
|
||||
value = tokens[index + 1][0]
|
||||
value, at = tokens[index + 1]
|
||||
index += 2
|
||||
if value == '{':
|
||||
value, index = block(index, True)
|
||||
@@ -70,9 +74,10 @@ def parse(text):
|
||||
else:
|
||||
raise ValueError("invalid config value")
|
||||
pairs.append((key, value))
|
||||
offsets.append(at)
|
||||
if nested:
|
||||
raise ValueError("unclosed config block")
|
||||
return Block(pairs, len(text)), index
|
||||
return Block(pairs, len(text), offsets), index
|
||||
|
||||
return block(0)[0]
|
||||
|
||||
|
||||
@@ -520,39 +520,8 @@ ln -sfn "$STEAM_X64/ubuntu12_32" "$X64_HOME/.steam/bin32"
|
||||
ln -sfn "$STEAM_X64/ubuntu12_64" "$X64_HOME/.steam/bin64"
|
||||
ln -sfn "$STEAM_X64/linux32" "$X64_HOME/.steam/sdk32"
|
||||
ln -sfn "$STEAM_X64/linux64" "$X64_HOME/.steam/sdk64"
|
||||
# The client's bwrap probe (steam-runtime-check-requirements) needs user namespaces, which no Android app has.
|
||||
srt_real=$STEAM_X64/steamrt64/pv-runtime/steam-runtime-steamrt
|
||||
srt_shim=$X64_HOME/steamrt-shim
|
||||
if [ -d "$srt_real" ]; then
|
||||
rm -rf "$srt_shim"
|
||||
mkdir -p "$srt_shim/bin"
|
||||
for e in "$srt_real"/* "$srt_real"/.[!.]*; do
|
||||
[ -e "$e" ] || continue
|
||||
[ "$(basename "$e")" = bin ] || ln -s "$e" "$srt_shim/$(basename "$e")"
|
||||
done
|
||||
for e in "$srt_real"/bin/*; do ln -s "$e" "$srt_shim/bin/$(basename "$e")"; done
|
||||
rm -f "$srt_shim/bin/steam-runtime-check-requirements"
|
||||
printf '#!/bin/sh
|
||||
exit 0
|
||||
' > "$srt_shim/bin/steam-runtime-check-requirements"
|
||||
chmod 755 "$srt_shim/bin/steam-runtime-check-requirements"
|
||||
rm -f "$srt_shim/bin/steam-runtime-launch-client"
|
||||
cat > "$srt_shim/bin/steam-runtime-launch-client" <<'LAUNCH'
|
||||
#!/bin/sh
|
||||
while [ $# -gt 0 ]; do
|
||||
case $1 in
|
||||
--) shift; break ;;
|
||||
--directory=*) cd "${1#--directory=}" || exit 127 ;;
|
||||
--env=*) export "${1#--env=}" ;;
|
||||
--unset-env=*) unset "${1#--unset-env=}" ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
[ $# -gt 0 ] || exit 0
|
||||
exec "$@"
|
||||
LAUNCH
|
||||
chmod 755 "$srt_shim/bin/steam-runtime-launch-client"
|
||||
fi
|
||||
# Valve's Steam Runtime container needs user namespaces and a session-bus launcher; steamrt-shim stands in for both.
|
||||
droiddeck-steamrt-shim "$STEAM_X64/steamrt64" "$X64_HOME/steamrt-shim" || rm -rf "$X64_HOME/steamrt-shim"
|
||||
# Valve's platform libraries ship without soname links (pressure-vessel makes them); ldconfig adds them here.
|
||||
srt_libs=$X64_HOME/steamrt-libs
|
||||
rm -rf "$srt_libs"
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
#!/bin/bash
|
||||
# droiddeck-steamrt-shim <client runtime dir> <shim dir>
|
||||
# Builds <shim dir> from the Steam Runtime container the client carries and exits 0, or removes it and exits 1
|
||||
# when the client carries none.
|
||||
set -eu
|
||||
client=$1
|
||||
shim=$2
|
||||
srt_real=$client/pv-runtime/steam-runtime-steamrt
|
||||
if [ ! -d "$srt_real" ]; then
|
||||
probe=$(find "$client" -maxdepth 4 -name steam-runtime-check-requirements -path '*/bin/*' -print -quit 2>/dev/null || true)
|
||||
srt_real=${probe:+${probe%/bin/*}}
|
||||
fi
|
||||
if [ -z "$srt_real" ] || [ ! -d "$srt_real" ]; then
|
||||
rm -rf "$shim"
|
||||
exit 1
|
||||
fi
|
||||
rm -rf "$shim"
|
||||
trap 'rm -rf "$shim"' ERR
|
||||
mkdir -p "$shim/bin"
|
||||
for e in "$srt_real"/* "$srt_real"/.[!.]*; do
|
||||
[ -e "$e" ] || continue
|
||||
[ "$(basename "$e")" = bin ] || ln -s "$e" "$shim/$(basename "$e")"
|
||||
done
|
||||
for e in "$srt_real"/bin/*; do
|
||||
[ -e "$e" ] || continue
|
||||
ln -s "$e" "$shim/bin/$(basename "$e")"
|
||||
done
|
||||
# The client's bwrap probe (steam-runtime-check-requirements) needs user namespaces, which no Android app has.
|
||||
rm -f "$shim/bin/steam-runtime-check-requirements"
|
||||
printf '#!/bin/sh
|
||||
exit 0
|
||||
' > "$shim/bin/steam-runtime-check-requirements"
|
||||
chmod 755 "$shim/bin/steam-runtime-check-requirements"
|
||||
# Valve's launcher asks a service on the session bus; proot is already the container, so the command runs here.
|
||||
rm -f "$shim/bin/steam-runtime-launch-client"
|
||||
cat > "$shim/bin/steam-runtime-launch-client" <<'LAUNCH'
|
||||
#!/bin/sh
|
||||
while [ $# -gt 0 ]; do
|
||||
case $1 in
|
||||
--) shift; break ;;
|
||||
--directory=*) cd "${1#--directory=}" || exit 127 ;;
|
||||
--env=*) export "${1#--env=}" ;;
|
||||
--unset-env=*) unset "${1#--unset-env=}" ;;
|
||||
--directory|--env|--unset-env)
|
||||
[ $# -ge 2 ] || exit 127
|
||||
case $1 in
|
||||
--directory) cd "$2" || exit 127 ;;
|
||||
--env) export "$2" ;;
|
||||
--unset-env) unset "$2" ;;
|
||||
esac
|
||||
shift ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
[ $# -gt 0 ] || exit 0
|
||||
exec "$@"
|
||||
LAUNCH
|
||||
chmod 755 "$shim/bin/steam-runtime-launch-client"
|
||||
+279
-74
@@ -17,6 +17,7 @@
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/timerfd.h>
|
||||
#include <time.h>
|
||||
#include <ucontext.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct ns_sem_args {
|
||||
@@ -68,12 +69,13 @@ struct ns_wait_args {
|
||||
#define NS_PROCS 256u
|
||||
#define NS_FDS 65536
|
||||
#define NS_DEVS 8
|
||||
#define NS_VERSION 1u
|
||||
#define NS_VERSION 2u
|
||||
#define NS_HEAD_MAGIC 0x31434e5953544e44ull
|
||||
#define NS_DEV_MAGIC 0x4e54445600000000ull
|
||||
#define NS_OBJ_MAGIC 0x27ull
|
||||
#define NS_WAITERS 0x80000000u
|
||||
#define NS_SWEEP_NS 2000000000ull
|
||||
#define NS_SPIN_NS 4000ull
|
||||
#define NS_DEAD 0xffffffffu
|
||||
|
||||
#ifndef CLOSE_RANGE_CLOEXEC
|
||||
@@ -144,6 +146,7 @@ struct ns_q {
|
||||
uint32_t waiting;
|
||||
uint32_t inuse;
|
||||
uint32_t next_free;
|
||||
uint32_t sleeping;
|
||||
struct ns_ent ent[NS_ENTS];
|
||||
};
|
||||
|
||||
@@ -168,6 +171,7 @@ struct ns_tls {
|
||||
struct ns_dev *d;
|
||||
uint32_t q;
|
||||
int waiting;
|
||||
int sleep;
|
||||
};
|
||||
|
||||
static struct ns_dev ns_devs[NS_DEVS];
|
||||
@@ -255,19 +259,148 @@ static void ns_unlock(uint32_t *w) {
|
||||
}
|
||||
}
|
||||
|
||||
static void ns_block(sigset_t *old) {
|
||||
sigset_t s;
|
||||
sigfillset(&s);
|
||||
sigdelset(&s, SIGSEGV);
|
||||
sigdelset(&s, SIGBUS);
|
||||
sigdelset(&s, SIGILL);
|
||||
sigdelset(&s, SIGFPE);
|
||||
sigdelset(&s, SIGTRAP);
|
||||
pthread_sigmask(SIG_BLOCK, &s, old);
|
||||
#define NS_DEFER 4
|
||||
#define NS_LEVELS 8
|
||||
|
||||
struct ns_cs {
|
||||
int depth;
|
||||
int count;
|
||||
int level;
|
||||
unsigned held_levels;
|
||||
int sigs[NS_DEFER];
|
||||
siginfo_t info[NS_DEFER];
|
||||
sigset_t held[NS_LEVELS];
|
||||
};
|
||||
|
||||
static __thread struct ns_cs ns_cs;
|
||||
static uintptr_t ns_handlers[_NSIG];
|
||||
static uint8_t ns_siginfo[_NSIG];
|
||||
|
||||
int bl_from_ntdll(const void *caller) __attribute__((visibility("hidden")));
|
||||
|
||||
static int ns_deferrable(int sig) {
|
||||
return sig > 0 && sig < _NSIG && sig != SIGKILL && sig != SIGSTOP && sig != SIGSEGV && sig != SIGBUS && sig != SIGILL
|
||||
&& sig != SIGFPE && sig != SIGTRAP && sig != SIGSYS;
|
||||
}
|
||||
|
||||
static void ns_restore(const sigset_t *old) {
|
||||
pthread_sigmask(SIG_SETMASK, old, NULL);
|
||||
static int ns_defer(int sig, const siginfo_t *si) {
|
||||
struct ns_cs *cs = &ns_cs;
|
||||
if (sig < SIGRTMIN)
|
||||
for (int i = 0; i < NS_DEFER; i++)
|
||||
if (__atomic_load_n(&cs->sigs[i], __ATOMIC_RELAXED) == sig) return 1;
|
||||
int i = __atomic_fetch_add(&cs->count, 1, __ATOMIC_RELAXED);
|
||||
if (i >= NS_DEFER) {
|
||||
__atomic_fetch_sub(&cs->count, 1, __ATOMIC_RELAXED);
|
||||
return 0;
|
||||
}
|
||||
if (si) memcpy(&cs->info[i], si, sizeof(*si));
|
||||
else memset(&cs->info[i], 0, sizeof(*si));
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
__atomic_store_n(&cs->sigs[i], sig, __ATOMIC_RELAXED);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int ns_park(int sig, const siginfo_t *si, ucontext_t *uc) {
|
||||
int level = ns_cs.level;
|
||||
if (level < 0 || level >= NS_LEVELS) return 0;
|
||||
sigset_t one;
|
||||
sigemptyset(&one);
|
||||
sigaddset(&one, sig);
|
||||
int saved = errno;
|
||||
pthread_sigmask(SIG_BLOCK, &one, NULL);
|
||||
pid_t pid = getpid(), tid = (pid_t)ns_self();
|
||||
int ok = (si && syscall(SYS_rt_tgsigqueueinfo, pid, tid, sig, si) == 0) || syscall(SYS_tgkill, pid, tid, sig) == 0;
|
||||
if (!ok) pthread_sigmask(SIG_UNBLOCK, &one, NULL);
|
||||
errno = saved;
|
||||
if (!ok) return 0;
|
||||
sigaddset(&uc->uc_sigmask, sig);
|
||||
unsigned long *words = (unsigned long *)&ns_cs.held[level];
|
||||
unsigned bits = 8 * sizeof(unsigned long);
|
||||
__atomic_fetch_or(&words[(unsigned)(sig - 1) / bits], 1ul << ((unsigned)(sig - 1) % bits), __ATOMIC_RELAXED);
|
||||
__atomic_fetch_or(&ns_cs.held_levels, 1u << level, __ATOMIC_RELAXED);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void ns_trampoline(int sig, siginfo_t *si, void *uc) {
|
||||
if (__atomic_load_n(&ns_cs.depth, __ATOMIC_RELAXED) > 0 && (ns_defer(sig, si) || (uc && ns_park(sig, si, uc)))) return;
|
||||
uintptr_t h = __atomic_load_n(&ns_handlers[sig], __ATOMIC_ACQUIRE);
|
||||
if (!h) return;
|
||||
ns_cs.level++;
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
if (__atomic_load_n(&ns_siginfo[sig], __ATOMIC_RELAXED)) ((void (*)(int, siginfo_t *, void *))h)(sig, si, uc);
|
||||
else ((void (*)(int))h)(sig);
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
ns_cs.level--;
|
||||
}
|
||||
|
||||
static void ns_flush(void) {
|
||||
struct ns_cs *cs = &ns_cs;
|
||||
sigset_t all, mask;
|
||||
sigfillset(&all);
|
||||
int saved = errno;
|
||||
pthread_sigmask(SIG_BLOCK, &all, &mask);
|
||||
int n = cs->count < NS_DEFER ? cs->count : NS_DEFER;
|
||||
pid_t pid = getpid(), tid = (pid_t)ns_self();
|
||||
for (int i = 0; i < n; i++)
|
||||
if (cs->sigs[i] > 0 && syscall(SYS_rt_tgsigqueueinfo, pid, tid, cs->sigs[i], &cs->info[i]) != 0) syscall(SYS_tgkill, pid, tid, cs->sigs[i]);
|
||||
memset(cs->sigs, 0, sizeof(cs->sigs));
|
||||
cs->count = 0;
|
||||
int level = cs->level;
|
||||
if (level >= 0 && level < NS_LEVELS && (cs->held_levels & (1u << level))) {
|
||||
for (int sig = 1; sig < _NSIG; sig++)
|
||||
if (sigismember(&cs->held[level], sig) == 1) sigdelset(&mask, sig);
|
||||
sigemptyset(&cs->held[level]);
|
||||
cs->held_levels &= ~(1u << level);
|
||||
}
|
||||
pthread_sigmask(SIG_SETMASK, &mask, NULL);
|
||||
errno = saved;
|
||||
}
|
||||
|
||||
static void ns_block(void) {
|
||||
ns_cs.depth++;
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
}
|
||||
|
||||
static void ns_restore(void) {
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
if (--ns_cs.depth == 0) {
|
||||
__atomic_signal_fence(__ATOMIC_SEQ_CST);
|
||||
if (ns_cs.count || (ns_cs.held_levels && ns_cs.level >= 0 && ns_cs.level < NS_LEVELS && (ns_cs.held_levels & (1u << ns_cs.level))))
|
||||
ns_flush();
|
||||
}
|
||||
}
|
||||
|
||||
int sigaction(int sig, const struct sigaction *act, struct sigaction *old) {
|
||||
static int (*real)(int, const struct sigaction *, struct sigaction *);
|
||||
if (!real) real = (int (*)(int, const struct sigaction *, struct sigaction *))dlsym(RTLD_NEXT, "sigaction");
|
||||
if (!real) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
if (!ns_deferrable(sig) || !ns_on()) return real(sig, act, old);
|
||||
uintptr_t prev = __atomic_load_n(&ns_handlers[sig], __ATOMIC_ACQUIRE);
|
||||
uint8_t prev_info = __atomic_load_n(&ns_siginfo[sig], __ATOMIC_RELAXED);
|
||||
struct sigaction mine;
|
||||
const struct sigaction *use = act;
|
||||
if (act && act->sa_handler != SIG_DFL && act->sa_handler != SIG_IGN && !(act->sa_flags & SA_RESETHAND) &&
|
||||
bl_from_ntdll(__builtin_return_address(0))) {
|
||||
mine = *act;
|
||||
mine.sa_sigaction = ns_trampoline;
|
||||
mine.sa_flags |= SA_SIGINFO;
|
||||
__atomic_store_n(&ns_siginfo[sig], (act->sa_flags & SA_SIGINFO) ? 1 : 0, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&ns_handlers[sig], (uintptr_t)act->sa_sigaction, __ATOMIC_RELEASE);
|
||||
use = &mine;
|
||||
}
|
||||
int r = real(sig, use, old);
|
||||
if (r != 0 && use == &mine) {
|
||||
__atomic_store_n(&ns_siginfo[sig], prev_info, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&ns_handlers[sig], prev, __ATOMIC_RELEASE);
|
||||
}
|
||||
if (r == 0 && old && old->sa_sigaction == ns_trampoline) {
|
||||
old->sa_sigaction = (void (*)(int, siginfo_t *, void *))prev;
|
||||
if (!prev_info) old->sa_flags &= ~SA_SIGINFO;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline struct ns_obj *ns_obj(struct ns_head *h, uint32_t s) {
|
||||
@@ -378,10 +511,11 @@ static int ns_claim(struct ns_q *q, int32_t index) {
|
||||
if (dead) return 0;
|
||||
}
|
||||
int32_t want = -1;
|
||||
return __atomic_compare_exchange_n(&q->signaled, &want, index, 0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED);
|
||||
return __atomic_compare_exchange_n(&q->signaled, &want, index, 0, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
static void ns_wake(struct ns_q *q) {
|
||||
if (!__atomic_load_n(&q->sleeping, __ATOMIC_SEQ_CST)) return;
|
||||
int saved = errno;
|
||||
ns_futex(&q->signaled, FUTEX_WAKE, 1, NULL, 0);
|
||||
errno = saved;
|
||||
@@ -618,14 +752,13 @@ static void ns_dev_put_locked(struct ns_dev *d) {
|
||||
|
||||
static void ns_dev_put(struct ns_dev *d) {
|
||||
if (__atomic_sub_fetch(&d->refs, 1, __ATOMIC_ACQ_REL)) return;
|
||||
sigset_t old;
|
||||
int saved = errno;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
pthread_mutex_lock(&ns_mx);
|
||||
uint32_t zero = 0;
|
||||
if (d->h && __atomic_compare_exchange_n(&d->refs, &zero, NS_DEAD, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) ns_release(d);
|
||||
pthread_mutex_unlock(&ns_mx);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
errno = saved;
|
||||
}
|
||||
|
||||
@@ -682,20 +815,18 @@ static void ns_forget_locked(int fd) {
|
||||
}
|
||||
|
||||
static void ns_forget(int fd) {
|
||||
sigset_t old;
|
||||
int saved = errno;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
pthread_mutex_lock(&ns_mx);
|
||||
ns_forget_locked(fd);
|
||||
pthread_mutex_unlock(&ns_mx);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
errno = saved;
|
||||
}
|
||||
|
||||
static uint64_t ns_adopt(struct ns_dev *d, uint32_t tag, int fd, uint32_t gen, uint32_t s) {
|
||||
uint64_t e = 0;
|
||||
sigset_t old;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
pthread_mutex_lock(&ns_mx);
|
||||
if (!d->h || d->tag != tag || !ns_table() || ns_attach(d) < 0 || s >= NS_SLOTS) goto out;
|
||||
if ((e = __atomic_load_n(&ns_fdt[fd], __ATOMIC_ACQUIRE))) goto out;
|
||||
@@ -710,7 +841,7 @@ static uint64_t ns_adopt(struct ns_dev *d, uint32_t tag, int fd, uint32_t gen, u
|
||||
ns_unlock(&o->lock);
|
||||
out:
|
||||
pthread_mutex_unlock(&ns_mx);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
return e;
|
||||
}
|
||||
|
||||
@@ -719,8 +850,7 @@ static uint64_t ns_note_device(int fd) {
|
||||
uint64_t e = 0;
|
||||
if (syscall(SYS_fstat, fd, &st) != 0 || !S_ISREG(st.st_mode)) return 0;
|
||||
if (((uint64_t)st.st_size & ~0xffffull) != NS_DEV_MAGIC) return 0;
|
||||
sigset_t old;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
pthread_mutex_lock(&ns_mx);
|
||||
struct ns_dev *d = ns_table() ? ns_map(fd, (uint32_t)st.st_size & 0xffff, (uint64_t)st.st_ino) : NULL;
|
||||
if (d) {
|
||||
@@ -732,7 +862,7 @@ static uint64_t ns_note_device(int fd) {
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&ns_mx);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
return e;
|
||||
}
|
||||
|
||||
@@ -773,15 +903,14 @@ static int ns_ready(struct ns_dev *d) {
|
||||
|
||||
static void ns_thread_gone(void *p) {
|
||||
struct ns_tls *t = p;
|
||||
if (!t || !t->d || !t->q) return;
|
||||
sigset_t old;
|
||||
if (!t || !t->d) return;
|
||||
struct ns_dev *d = t->d;
|
||||
ns_block(&old);
|
||||
ns_free_q(d->h, t->q - 1);
|
||||
ns_block();
|
||||
if (t->q) ns_free_q(d->h, t->q - 1);
|
||||
t->q = 0;
|
||||
t->waiting = 0;
|
||||
t->d = NULL;
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
ns_dev_put(d);
|
||||
}
|
||||
|
||||
@@ -789,24 +918,28 @@ static void ns_key_init(void) {
|
||||
pthread_key_create(&ns_key, ns_thread_gone);
|
||||
}
|
||||
|
||||
static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
|
||||
struct ns_head *h = d->h;
|
||||
if (ns_tq.d && ns_tq.q && ns_tq.waiting) {
|
||||
ns_drop_q(ns_tq.d->h, ns_tq.q - 1);
|
||||
ns_tq.waiting = 0;
|
||||
}
|
||||
if (ns_tq.d == d && ns_tq.q) {
|
||||
*qi = ns_tq.q - 1;
|
||||
return ns_q(h, *qi);
|
||||
}
|
||||
if (ns_tq.d && ns_tq.q) {
|
||||
struct ns_dev *old = ns_tq.d;
|
||||
ns_free_q(old->h, ns_tq.q - 1);
|
||||
static int ns_hold(struct ns_dev *d) {
|
||||
if (ns_tq.d == d) return 1;
|
||||
if (!ns_dev_get(d)) return 0;
|
||||
if (ns_tq.sleep) return 2;
|
||||
struct ns_dev *old = ns_tq.d;
|
||||
if (old) {
|
||||
ns_block();
|
||||
if (ns_tq.q) ns_free_q(old->h, ns_tq.q - 1);
|
||||
ns_tq.q = 0;
|
||||
ns_tq.waiting = 0;
|
||||
ns_tq.d = NULL;
|
||||
ns_restore();
|
||||
ns_dev_put(old);
|
||||
}
|
||||
pthread_once(&ns_once, ns_key_init);
|
||||
ns_tq.d = d;
|
||||
pthread_setspecific(ns_key, &ns_tq);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static struct ns_q *ns_new_q(struct ns_dev *d, uint32_t *qi) {
|
||||
struct ns_head *h = d->h;
|
||||
uint32_t i;
|
||||
if (!ns_pop(h, &h->q_free, 0, &i) && !ns_grow(&h->q_hwm, NS_QS, &i)) {
|
||||
ns_sweep_dead(h);
|
||||
@@ -815,22 +948,35 @@ static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
|
||||
struct ns_q *q = ns_q(h, i);
|
||||
q->proc = (uint32_t)d->proc;
|
||||
q->waiting = 0;
|
||||
q->sleeping = 0;
|
||||
q->inuse = 1;
|
||||
__atomic_add_fetch(&d->refs, 1, __ATOMIC_ACQ_REL);
|
||||
ns_tq.d = d;
|
||||
ns_tq.q = i + 1;
|
||||
ns_tq.waiting = 0;
|
||||
pthread_setspecific(ns_key, &ns_tq);
|
||||
*qi = i;
|
||||
return q;
|
||||
}
|
||||
|
||||
static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) {
|
||||
struct ns_head *h = d->h;
|
||||
if (ns_tq.d != d) return NULL;
|
||||
if (ns_tq.q && ns_tq.waiting) {
|
||||
ns_drop_q(h, ns_tq.q - 1);
|
||||
ns_tq.waiting = 0;
|
||||
}
|
||||
if (ns_tq.q) {
|
||||
*qi = ns_tq.q - 1;
|
||||
return ns_q(h, *qi);
|
||||
}
|
||||
struct ns_q *q = ns_new_q(d, qi);
|
||||
if (!q) return NULL;
|
||||
ns_tq.q = *qi + 1;
|
||||
ns_tq.waiting = 0;
|
||||
return q;
|
||||
}
|
||||
|
||||
static int ns_create(struct ns_dev *d, int type, uint32_t a, uint32_t b) {
|
||||
struct ns_head *h = d->h;
|
||||
sigset_t old;
|
||||
int fd = -1, err = 0;
|
||||
uint32_t s = 0;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
pthread_mutex_lock(&ns_mx);
|
||||
if (!ns_table() || ns_attach(d) < 0) {
|
||||
err = ENOMEM;
|
||||
@@ -879,7 +1025,7 @@ static int ns_create(struct ns_dev *d, int type, uint32_t a, uint32_t b) {
|
||||
ns_unlock(&o->lock);
|
||||
out:
|
||||
pthread_mutex_unlock(&ns_mx);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
if (fd < 0) errno = err;
|
||||
return fd;
|
||||
}
|
||||
@@ -890,6 +1036,20 @@ static int ns_expired(const struct ns_wait_args *args) {
|
||||
return (uint64_t)now.tv_sec * 1000000000ull + (uint64_t)now.tv_nsec >= args->timeout;
|
||||
}
|
||||
|
||||
static uint64_t ns_now(void) {
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
return (uint64_t)now.tv_sec * 1000000000ull + (uint64_t)now.tv_nsec;
|
||||
}
|
||||
|
||||
static void ns_spin(struct ns_q *q) {
|
||||
uint64_t end = ns_now() + NS_SPIN_NS;
|
||||
for (unsigned i = 1; __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1; i++) {
|
||||
ns_relax();
|
||||
if (!(i & 63) && ns_now() >= end) return;
|
||||
}
|
||||
}
|
||||
|
||||
static int ns_sleep(struct ns_q *q, const struct ns_wait_args *args) {
|
||||
struct timespec ts, *tsp = NULL;
|
||||
int op = FUTEX_WAIT_BITSET | ((args->flags & NS_WAIT_REALTIME) ? FUTEX_CLOCK_REALTIME : 0);
|
||||
@@ -898,14 +1058,45 @@ static int ns_sleep(struct ns_q *q, const struct ns_wait_args *args) {
|
||||
ts.tv_nsec = (long)(args->timeout % 1000000000ull);
|
||||
tsp = &ts;
|
||||
}
|
||||
while (__atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1) {
|
||||
if (tsp && ns_expired(args)) return ETIMEDOUT;
|
||||
if (ns_futex(&q->signaled, op, (uint32_t)-1, tsp, FUTEX_BITSET_MATCH_ANY) == -1) {
|
||||
if (errno == ETIMEDOUT) return ETIMEDOUT;
|
||||
if (errno == EINTR) return EINTR;
|
||||
}
|
||||
ns_spin(q);
|
||||
int ret = 0;
|
||||
__atomic_store_n(&q->sleeping, 1, __ATOMIC_SEQ_CST);
|
||||
while (!ret && __atomic_load_n(&q->signaled, __ATOMIC_SEQ_CST) == -1) {
|
||||
if (tsp && ns_expired(args)) ret = ETIMEDOUT;
|
||||
else if (ns_futex(&q->signaled, op, (uint32_t)-1, tsp, FUTEX_BITSET_MATCH_ANY) == -1 && (errno == ETIMEDOUT || errno == EINTR))
|
||||
ret = errno;
|
||||
}
|
||||
return 0;
|
||||
__atomic_store_n(&q->sleeping, 0, __ATOMIC_RELAXED);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int ns_try_any(struct ns_head *h, const uint32_t *slots, const uint32_t *gens, uint32_t total, uint32_t owner, int *dead) {
|
||||
for (uint32_t i = 0; i < total; i++) {
|
||||
struct ns_obj *o = ns_obj(h, slots[i]);
|
||||
ns_lock(&o->lock);
|
||||
int busy = o->type == NS_FREE || o->gen != gens[i] || o->any_head || o->all_hint;
|
||||
if (!busy && ns_signaled(o, owner)) {
|
||||
switch (o->type) {
|
||||
case NS_SEM:
|
||||
o->a--;
|
||||
break;
|
||||
case NS_MUTEX:
|
||||
*dead = (int)o->ownerdead;
|
||||
o->ownerdead = 0;
|
||||
o->a++;
|
||||
o->b = owner;
|
||||
break;
|
||||
case NS_EVENT:
|
||||
if (!o->a) o->b = 0;
|
||||
break;
|
||||
}
|
||||
ns_unlock(&o->lock);
|
||||
return (int)i;
|
||||
}
|
||||
ns_unlock(&o->lock);
|
||||
if (busy) return -1;
|
||||
}
|
||||
return -2;
|
||||
}
|
||||
|
||||
static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
|
||||
@@ -927,12 +1118,24 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
|
||||
for (uint32_t j = 0; j < i; j++)
|
||||
if (slots[j] == slots[i]) return EINVAL;
|
||||
}
|
||||
sigset_t old;
|
||||
ns_block(&old);
|
||||
int nested = ns_tq.sleep > 0;
|
||||
ns_block();
|
||||
if (!all && (nested || !ns_tq.waiting)) {
|
||||
int dead = 0, got = ns_try_any(h, slots, gens, total, args.owner, &dead);
|
||||
if (got >= 0) {
|
||||
ns_restore();
|
||||
ua->index = (uint32_t)got;
|
||||
return dead ? EOWNERDEAD : 0;
|
||||
}
|
||||
if (got == -2 && args.timeout != UINT64_MAX && ns_expired(&args)) {
|
||||
ns_restore();
|
||||
return ETIMEDOUT;
|
||||
}
|
||||
}
|
||||
uint32_t qi;
|
||||
struct ns_q *q = ns_ready(d) ? ns_thread_q(d, &qi) : NULL;
|
||||
struct ns_q *q = !ns_ready(d) ? NULL : nested ? ns_new_q(d, &qi) : ns_thread_q(d, &qi);
|
||||
if (!q) {
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
return ENOMEM;
|
||||
}
|
||||
ns_maybe_sweep(h);
|
||||
@@ -952,7 +1155,7 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
|
||||
}
|
||||
q->total = total;
|
||||
q->waiting = 1;
|
||||
ns_tq.waiting = 1;
|
||||
if (!nested) ns_tq.waiting = 1;
|
||||
int bad = 0;
|
||||
if (!all) {
|
||||
for (uint32_t i = 0; i < total && !bad; i++) {
|
||||
@@ -1004,16 +1207,18 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) {
|
||||
}
|
||||
int ret = 0;
|
||||
if (!bad && __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1) {
|
||||
sigset_t held;
|
||||
ns_restore(&old);
|
||||
ns_tq.sleep++;
|
||||
ns_restore();
|
||||
ret = ns_sleep(q, &args);
|
||||
ns_block(&held);
|
||||
ns_block();
|
||||
ns_tq.sleep--;
|
||||
}
|
||||
ns_drop_q(h, qi);
|
||||
ns_tq.waiting = 0;
|
||||
if (!nested) ns_tq.waiting = 0;
|
||||
int32_t got = __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE);
|
||||
int dead = (int)q->ownerdead;
|
||||
ns_restore(&old);
|
||||
if (nested) ns_free_q(h, qi);
|
||||
ns_restore();
|
||||
if (bad) return EINVAL;
|
||||
if (got != -1) {
|
||||
ua->index = (uint32_t)got;
|
||||
@@ -1061,12 +1266,11 @@ static int ns_obj_ioctl(struct ns_dev *d, uint64_t e, unsigned long req, void *a
|
||||
return ENOTTY;
|
||||
}
|
||||
if (type != (uint32_t)want) return EINVAL;
|
||||
sigset_t old;
|
||||
ns_block(&old);
|
||||
ns_block();
|
||||
int all = ns_lock_obj(h, o);
|
||||
if (o->type != (uint32_t)want || o->gen != gen) {
|
||||
ns_unlock_obj(h, o, all);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
return EINVAL;
|
||||
}
|
||||
switch (req) {
|
||||
@@ -1133,7 +1337,7 @@ static int ns_obj_ioctl(struct ns_dev *d, uint64_t e, unsigned long req, void *a
|
||||
}
|
||||
}
|
||||
ns_unlock_obj(h, o, all);
|
||||
ns_restore(&old);
|
||||
ns_restore();
|
||||
if (ret) return ret;
|
||||
if (req == NS_SEM_RELEASE || req == NS_EVENT_SET || req == NS_EVENT_RESET || req == NS_EVENT_PULSE)
|
||||
memcpy(arg, &prev, sizeof(prev));
|
||||
@@ -1176,9 +1380,10 @@ int bl_ntsync_ioctl(int fd, unsigned long req, void *arg, int *rc) {
|
||||
uint64_t e = ns_lookup(fd);
|
||||
if (!e) return 0;
|
||||
struct ns_dev *d = &ns_devs[(e >> 56) & 7];
|
||||
if (!ns_dev_get(d)) return 0;
|
||||
int held = ns_hold(d);
|
||||
if (!held) return 0;
|
||||
int out = 0, err = (e & NS_E_DEVICE) ? ns_dev_ioctl(d, req, arg, &out) : ns_obj_ioctl(d, e, req, arg);
|
||||
ns_dev_put(d);
|
||||
if (held == 2) ns_dev_put(d);
|
||||
if (err) {
|
||||
errno = err;
|
||||
*rc = -1;
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
#define STATUS_MAX 16384
|
||||
#define TRACER_FIELD "\nTracerPid:\t"
|
||||
|
||||
int bl_from_ntdll(const void *caller) __attribute__((visibility("hidden")));
|
||||
|
||||
static const char *skip_digits(const char *s) {
|
||||
const char *end = s;
|
||||
while (isdigit((unsigned char)*end)) end++;
|
||||
@@ -77,7 +79,8 @@ __attribute__((visibility("hidden"))) int bl_status_without_tracer(const char *p
|
||||
int out = -1;
|
||||
|
||||
long pid;
|
||||
if ((flags & O_ACCMODE) != O_RDONLY || (pid = status_pid(path)) < 0 || is_web_helper(caller)) return -1;
|
||||
if ((flags & O_ACCMODE) != O_RDONLY || (pid = status_pid(path)) < 0 || is_web_helper(caller) || bl_from_ntdll(caller))
|
||||
return -1;
|
||||
int in = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC);
|
||||
if (in < 0) goto done;
|
||||
for (;;) {
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <dlfcn.h>
|
||||
#include <link.h>
|
||||
#include <signal.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/auxv.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <ucontext.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static uintptr_t ntdll_lo, ntdll_hi;
|
||||
|
||||
static int find_ntdll(struct dl_phdr_info *info, size_t size, void *data) {
|
||||
(void)size;
|
||||
(void)data;
|
||||
const char *slash = strrchr(info->dlpi_name, '/');
|
||||
if (strcmp(slash ? slash + 1 : info->dlpi_name, "ntdll.so") != 0) return 0;
|
||||
for (int i = 0; i < info->dlpi_phnum; i++) {
|
||||
const ElfW(Phdr) *ph = &info->dlpi_phdr[i];
|
||||
if (ph->p_type != PT_LOAD || !(ph->p_flags & PF_X)) continue;
|
||||
uintptr_t lo = info->dlpi_addr + ph->p_vaddr;
|
||||
__atomic_store_n(&ntdll_hi, lo + ph->p_memsz, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&ntdll_lo, lo, __ATOMIC_RELEASE);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) int bl_from_ntdll(const void *caller) {
|
||||
uintptr_t pc = (uintptr_t)caller, lo = __atomic_load_n(&ntdll_lo, __ATOMIC_ACQUIRE);
|
||||
if (!lo) {
|
||||
dl_iterate_phdr(find_ntdll, NULL);
|
||||
lo = __atomic_load_n(&ntdll_lo, __ATOMIC_ACQUIRE);
|
||||
if (!lo) return 0;
|
||||
}
|
||||
return pc >= lo && pc < __atomic_load_n(&ntdll_hi, __ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
#if defined(__aarch64__)
|
||||
#include <asm/hwcap.h>
|
||||
#include <asm/sigcontext.h>
|
||||
|
||||
#define BL_STR2(x) #x
|
||||
#define BL_STR(x) BL_STR2(x)
|
||||
|
||||
#define BL_HWCAP_GCS (1UL << 32)
|
||||
#define BL_FRAME_SIZE 4688
|
||||
#define BL_REGS 312
|
||||
#define BL_SP 560
|
||||
#define BL_PC 568
|
||||
#define BL_PSTATE 576
|
||||
#define BL_FPSIMD 592
|
||||
|
||||
struct bl_sigframe {
|
||||
siginfo_t info;
|
||||
ucontext_t uc;
|
||||
};
|
||||
|
||||
struct bl_kernel_sigaction {
|
||||
void (*handler)(int, siginfo_t *, void *);
|
||||
unsigned long flags;
|
||||
void *restorer;
|
||||
uint64_t mask;
|
||||
};
|
||||
|
||||
struct bl_usr2_call {
|
||||
void (*handler)(int, siginfo_t *, void *);
|
||||
uint64_t old_mask;
|
||||
};
|
||||
|
||||
_Static_assert(sizeof(struct bl_sigframe) == BL_FRAME_SIZE, "rt_sigframe size");
|
||||
_Static_assert(offsetof(struct bl_sigframe, uc.uc_mcontext.regs) == BL_REGS, "regs offset");
|
||||
_Static_assert(offsetof(struct bl_sigframe, uc.uc_mcontext.sp) == BL_SP, "sp offset");
|
||||
_Static_assert(offsetof(struct bl_sigframe, uc.uc_mcontext.pc) == BL_PC, "pc offset");
|
||||
_Static_assert(offsetof(struct bl_sigframe, uc.uc_mcontext.pstate) == BL_PSTATE, "pstate offset");
|
||||
_Static_assert(offsetof(struct bl_sigframe, uc.uc_mcontext.__reserved) == BL_FPSIMD, "reserved offset");
|
||||
_Static_assert(sizeof(struct fpsimd_context) + sizeof(struct _aarch64_ctx) <=
|
||||
sizeof(((mcontext_t *)0)->__reserved), "reserved space");
|
||||
|
||||
__attribute__((visibility("hidden"))) void bl_usr2_frame(struct bl_usr2_call *call);
|
||||
__attribute__((visibility("hidden"), used)) void bl_usr2_run(struct bl_usr2_call *call, struct bl_sigframe *frame);
|
||||
|
||||
__asm__(".text\n"
|
||||
".p2align 2\n"
|
||||
".type bl_usr2_frame, %function\n"
|
||||
"bl_usr2_frame:\n"
|
||||
" mov x16, #" BL_STR(BL_FRAME_SIZE) "\n"
|
||||
" sub sp, sp, x16\n"
|
||||
" add x17, sp, #" BL_STR(BL_REGS) "\n"
|
||||
" stp x0, x1, [x17, #0]\n"
|
||||
" stp x2, x3, [x17, #16]\n"
|
||||
" stp x4, x5, [x17, #32]\n"
|
||||
" stp x6, x7, [x17, #48]\n"
|
||||
" stp x8, x9, [x17, #64]\n"
|
||||
" stp x10, x11, [x17, #80]\n"
|
||||
" stp x12, x13, [x17, #96]\n"
|
||||
" stp x14, x15, [x17, #112]\n"
|
||||
" stp xzr, xzr, [x17, #128]\n"
|
||||
" stp x18, x19, [x17, #144]\n"
|
||||
" stp x20, x21, [x17, #160]\n"
|
||||
" stp x22, x23, [x17, #176]\n"
|
||||
" stp x24, x25, [x17, #192]\n"
|
||||
" stp x26, x27, [x17, #208]\n"
|
||||
" stp x28, x29, [x17, #224]\n"
|
||||
" str x30, [x17, #240]\n"
|
||||
" str xzr, [x17, #0]\n"
|
||||
" add x16, sp, x16\n"
|
||||
" str x16, [sp, #" BL_STR(BL_SP) "]\n"
|
||||
" adr x16, 1f\n"
|
||||
" str x16, [sp, #" BL_STR(BL_PC) "]\n"
|
||||
" mrs x16, nzcv\n"
|
||||
" str x16, [sp, #" BL_STR(BL_PSTATE) "]\n"
|
||||
" add x17, sp, #" BL_STR(BL_FPSIMD) "\n"
|
||||
" mrs x16, fpsr\n"
|
||||
" str w16, [x17, #8]\n"
|
||||
" mrs x16, fpcr\n"
|
||||
" str w16, [x17, #12]\n"
|
||||
" stp q0, q1, [x17, #16]\n"
|
||||
" stp q2, q3, [x17, #48]\n"
|
||||
" stp q4, q5, [x17, #80]\n"
|
||||
" stp q6, q7, [x17, #112]\n"
|
||||
" stp q8, q9, [x17, #144]\n"
|
||||
" stp q10, q11, [x17, #176]\n"
|
||||
" stp q12, q13, [x17, #208]\n"
|
||||
" stp q14, q15, [x17, #240]\n"
|
||||
" stp q16, q17, [x17, #272]\n"
|
||||
" stp q18, q19, [x17, #304]\n"
|
||||
" stp q20, q21, [x17, #336]\n"
|
||||
" stp q22, q23, [x17, #368]\n"
|
||||
" stp q24, q25, [x17, #400]\n"
|
||||
" stp q26, q27, [x17, #432]\n"
|
||||
" stp q28, q29, [x17, #464]\n"
|
||||
" stp q30, q31, [x17, #496]\n"
|
||||
" mov x1, sp\n"
|
||||
" bl bl_usr2_run\n"
|
||||
" mov x8, #" BL_STR(__NR_rt_sigreturn) "\n"
|
||||
" svc #0\n"
|
||||
" brk #0\n"
|
||||
"1:\n"
|
||||
" ret\n"
|
||||
".size bl_usr2_frame, .-bl_usr2_frame\n");
|
||||
|
||||
static int (*usr2_real_raise)(int);
|
||||
|
||||
static long usr2_sys(long nr, long a, long b, long c, long d) {
|
||||
register long x8 __asm__("x8") = nr;
|
||||
register long x0 __asm__("x0") = a;
|
||||
register long x1 __asm__("x1") = b;
|
||||
register long x2 __asm__("x2") = c;
|
||||
register long x3 __asm__("x3") = d;
|
||||
__asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3) : "memory");
|
||||
return x0;
|
||||
}
|
||||
|
||||
static uint64_t usr2_ssbs(void) {
|
||||
uint64_t value = 0;
|
||||
if (getauxval(AT_HWCAP) & HWCAP_SSBS) __asm__ volatile("mrs %0, s3_3_c4_c2_6" : "=r"(value));
|
||||
return value & (1UL << 12);
|
||||
}
|
||||
|
||||
void bl_usr2_run(struct bl_usr2_call *call, struct bl_sigframe *frame) {
|
||||
struct fpsimd_context *fp = (struct fpsimd_context *)frame->uc.uc_mcontext.__reserved;
|
||||
struct _aarch64_ctx *end = (struct _aarch64_ctx *)(fp + 1);
|
||||
|
||||
memset(&frame->info, 0, sizeof(frame->info));
|
||||
frame->info.si_signo = SIGUSR2;
|
||||
frame->info.si_code = SI_TKILL;
|
||||
frame->info.si_pid = (pid_t)usr2_sys(__NR_getpid, 0, 0, 0, 0);
|
||||
frame->info.si_uid = (uid_t)usr2_sys(__NR_getuid, 0, 0, 0, 0);
|
||||
frame->uc.uc_flags = 0;
|
||||
frame->uc.uc_link = NULL;
|
||||
if (usr2_sys(__NR_sigaltstack, 0, (long)&frame->uc.uc_stack, 0, 0) != 0) {
|
||||
memset(&frame->uc.uc_stack, 0, sizeof(frame->uc.uc_stack));
|
||||
frame->uc.uc_stack.ss_flags = SS_DISABLE;
|
||||
}
|
||||
memset(&frame->uc.uc_sigmask, 0, sizeof(frame->uc.uc_sigmask));
|
||||
memcpy(&frame->uc.uc_sigmask, &call->old_mask, sizeof(call->old_mask));
|
||||
frame->uc.uc_mcontext.fault_address = 0;
|
||||
frame->uc.uc_mcontext.pstate |= usr2_ssbs();
|
||||
fp->head.magic = FPSIMD_MAGIC;
|
||||
fp->head.size = sizeof(*fp);
|
||||
end->magic = 0;
|
||||
end->size = 0;
|
||||
call->handler(SIGUSR2, &frame->info, &frame->uc);
|
||||
}
|
||||
|
||||
int raise(int sig) {
|
||||
int (*real)(int) = __atomic_load_n(&usr2_real_raise, __ATOMIC_ACQUIRE);
|
||||
if (!real) {
|
||||
real = (int (*)(int))dlsym(RTLD_NEXT, "raise");
|
||||
__atomic_store_n(&usr2_real_raise, real, __ATOMIC_RELEASE);
|
||||
}
|
||||
if (sig != SIGUSR2 || (getauxval(AT_HWCAP) & BL_HWCAP_GCS) || !bl_from_ntdll(__builtin_return_address(0))) {
|
||||
return real(sig);
|
||||
}
|
||||
|
||||
struct bl_kernel_sigaction action;
|
||||
if (usr2_sys(__NR_rt_sigaction, SIGUSR2, 0, (long)&action, sizeof(action.mask)) != 0) return real(sig);
|
||||
if (!(action.flags & SA_SIGINFO) || (action.flags & SA_RESETHAND) || (uintptr_t)action.handler <= 1) {
|
||||
return real(sig);
|
||||
}
|
||||
|
||||
uint64_t bit = 1ULL << (SIGUSR2 - 1);
|
||||
uint64_t block = action.mask | ((action.flags & SA_NODEFER) ? 0 : bit);
|
||||
struct bl_usr2_call call = {action.handler, 0};
|
||||
if (usr2_sys(__NR_rt_sigprocmask, SIG_BLOCK, (long)&block, (long)&call.old_mask, sizeof(block)) != 0) {
|
||||
return real(sig);
|
||||
}
|
||||
if (call.old_mask & bit) {
|
||||
usr2_sys(__NR_rt_sigprocmask, SIG_SETMASK, (long)&call.old_mask, 0, sizeof(call.old_mask));
|
||||
return real(sig);
|
||||
}
|
||||
bl_usr2_frame(&call);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
Reference in new issue
Block a user