From nobody Sun Feb 8 21:21:23 2026 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D14D32C0F79 for ; Wed, 4 Feb 2026 02:28:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770172111; cv=none; b=r4pfUVIyB+vKhDlTnjXH1nRp1Mz5Vlo2hlg/P5V4gpSjtYw6oikr1efiw5OLqEDV+fQND6ticRYHki7MH0KpSGSYVcOf/hD+DeNEH8TLpAnfOhN0+4ttDc9V5dMftKu34CUc2K+eTiCsFKnSIqofRSe6kFpTsEmahI/lw6hKwiA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770172111; c=relaxed/simple; bh=Smx444R1K3eGvehQLSYSKsoNfFVfI8EFubuS5ARqwWk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Lr2y6fac7VEzdFPQDwkLCnz0z6N3mS5kB/GUP6yuaLGzo+k9jXbFkKex6lJgOMmEwRYoPR61UbuTuikcG/fU+rGha2cbaeeNdDzx9949tQwxp+33A7rZPQYipnhYZ30N1qT6uB0+gUaDLtHngEPCZTGu4XC8u8ZtSvbsyzKVGLU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=dLyLRS7o; arc=none smtp.client-ip=209.85.216.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dLyLRS7o" Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-34abc7da414so3885730a91.0 for ; Tue, 03 Feb 2026 18:28:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770172109; x=1770776909; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=EozC+O+V6UtpPMZ43xTGpDpBr5Y3IUusGEEQ6C8VmaE=; b=dLyLRS7oLvYYFUg5gaaUTYGOyP15fBlio5JcfRy7w4JC7VlYwJGLqw+rpFRz8MTiwn 09u9N9Dtlt07sd+FFomHNIgBpqDM4YEIlVwNMcebub+b8+7WZR82CGjuZgnsMudr/AVu YWoroJzC2SjDD/hTJl59z0TUgLwReCGaxGn7dQIcZuGVgjV17sf7ulLRmpW+8YwqQT7Z R7x5vzqyR9pkctHyDpUUzs2zEsBAUwiBjV5X5vKRGYPZd6SQzJeH1FNP6Ucl/9Z9DOgX CHT7XJCQCLZ9Be4hx9Ms270320XC+FnBhv7O00aPdzLzelQPEAuLNZslMU+6KADlIYW0 9r9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770172109; x=1770776909; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=EozC+O+V6UtpPMZ43xTGpDpBr5Y3IUusGEEQ6C8VmaE=; b=hV8gKhLAjX7oJWdVZqfBgTOZPPSqdJnX9luSvhVTQKNBbfmXFlgqIc5CKy6IXAzre2 Q2dgJzcqLpV7ed5Db4M676Z3ftZl593rAOEBwJTQsof82Y5pBec1BzC/OA1L6meV3dqc nWf14Q+SEf1w/GYLz1Bwo2IxfUJTA7BsYec6cyAb0Xqpu0vEYXALOqsR8gSvtVt6Zvem Tb5TsMiqIHLSaYgmXYUtws3KpweKxvQTsE/DWub4cUxdxdJzV234t0ivwj+CR4IZFgXu MnbmzQT8CbV9ReJVMRbEPVxk23MJbVi/4m1MJQkggteu3qJLJbwRw78tttCacnbcn7Z1 t0CQ== X-Forwarded-Encrypted: i=1; AJvYcCU+L+yHoYu2iD1wwG+NMtp4xH64GJVnDdUwBjy5f1WApDHZtn/cyikBJHeY+qHqaecRSu6HKpxfGTjgGz0=@vger.kernel.org X-Gm-Message-State: AOJu0Yzt0mvPBSKrpDekKULGHjXq1uIRiTYtIrwLT/HobP4UM22cns0c /1vjcFam7PLFLxspNBUmDbczOioSwcc5BC0SZSIMfGf1Ph911d70EOVO X-Gm-Gg: AZuq6aL7oA5UXhqfboPlNIhsFjBty9Ut1UvdI3gmK1pHUKPUQtb8SE0Y7nGcEZGuv7a ItrVqXCQQ1nVefHRB8EA92HIsNr6jicvAhC7mYHMXDGGGqQd96pSpT6PE4MbjISOIo0MES94QNh XsOjFS/o5I1/0CL/iVqC8qNnJilrGxVYl2G2saIy61XtbqS/PSLSEdp45gWyX9/5zXXTBLCAVRP jRux9q/x+Mq3noJy0MBQks+r++DA5b2CT4ErZGtDcf0pzrv4Wc2yZ6luK8ce7mijlUjy3skFueT cCK0j7C0vv1QBaTpTHRKM8tt/KMRa3ENq3GIYZwQ4/TQC6TDZCeFPaBPZMFfOmCH0OhH0rkeJEj LBLkh90GTBnD7Ea4nFwH1kNNLSKNalcymS2tvbnyBIgHMBCTmJHr6kXagNMhf/VEiZdqlKYom9X mJ7TIee2VjhPkDIP9PkN+9nckps5cO8j9ouk4HjzrDRYghJXLTdbwgVDEkQbdyupXBx10VFilRb wLiZT9wKhQVsxE4 X-Received: by 2002:a17:90b:3884:b0:340:4abf:391d with SMTP id 98e67ed59e1d1-35487125d82mr1194529a91.16.1770172109005; Tue, 03 Feb 2026 18:28:29 -0800 (PST) Received: from ikb-h07-29-noble.in.iijlab.net ([202.214.97.5]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-35485da24a4sm874683a91.1.2026.02.03.18.28.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Feb 2026 18:28:26 -0800 (PST) Received: by ikb-h07-29-noble.in.iijlab.net (Postfix, from userid 1010) id 52A6A1200C3D; Wed, 4 Feb 2026 11:28:25 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Cc: thehajime@gmail.com, ricarkol@google.com, Liam.Howlett@oracle.com, linux-kernel@vger.kernel.org Subject: [PATCH v14 07/13] um: nommu: configure fs register on host syscall invocation Date: Wed, 4 Feb 2026 11:28:05 +0900 Message-ID: <6cbe7389d6f8250a22afe2213c90a6ceaf721a97.1770170302.git.thehajime@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" As userspace on UML/!MMU also need to configure %fs register when it is running to correctly access thread structure, host syscalls implemented in os-Linux drivers may be puzzled when they are called. Thus it has to configure %fs register via arch_prctl(SET_FS) on every host syscalls. Signed-off-by: Hajime Tazaki Signed-off-by: Ricardo Koller --- arch/um/include/shared/os.h | 6 ++++ arch/um/os-Linux/process.c | 6 ++++ arch/um/os-Linux/start_up.c | 21 ++++++++++++++ arch/x86/um/nommu/do_syscall_64.c | 11 +++++++ arch/x86/um/nommu/syscalls.h | 1 + arch/x86/um/nommu/syscalls_64.c | 48 ++++++++++++++++++++++++++++++- 6 files changed, 92 insertions(+), 1 deletion(-) diff --git a/arch/um/include/shared/os.h b/arch/um/include/shared/os.h index 5451f9b1f41e..0ac87507e05e 100644 --- a/arch/um/include/shared/os.h +++ b/arch/um/include/shared/os.h @@ -189,6 +189,7 @@ extern void check_host_supports_tls(int *supports_tls, = int *tls_min); extern void get_host_cpu_features( void (*flags_helper_func)(char *line), void (*cache_helper_func)(char *line)); +extern int host_has_fsgsbase; =20 /* mem.c */ extern int create_mem_file(unsigned long long len); @@ -213,6 +214,11 @@ extern int os_protect_memory(void *addr, unsigned long= len, extern int os_unmap_memory(void *addr, int len); extern int os_drop_memory(void *addr, int length); extern int can_drop_memory(void); +extern int os_arch_prctl(int pid, int option, unsigned long *arg); +#ifndef CONFIG_MMU +extern long long host_fs; +#endif + =20 void os_set_pdeathsig(void); =20 diff --git a/arch/um/os-Linux/process.c b/arch/um/os-Linux/process.c index b338a89ecf1e..4f30a389c530 100644 --- a/arch/um/os-Linux/process.c +++ b/arch/um/os-Linux/process.c @@ -18,6 +18,7 @@ #include #include #include +#include /* For SYS_xxx definitions */ #include #include #include @@ -179,6 +180,11 @@ int __init can_drop_memory(void) return ok; } =20 +int os_arch_prctl(int pid, int option, unsigned long *arg2) +{ + return syscall(SYS_arch_prctl, option, arg2); +} + void init_new_thread_signals(void) { set_handler(SIGSEGV); diff --git a/arch/um/os-Linux/start_up.c b/arch/um/os-Linux/start_up.c index 33e039d2c1bf..c0afe5d8b559 100644 --- a/arch/um/os-Linux/start_up.c +++ b/arch/um/os-Linux/start_up.c @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include #include #include @@ -37,6 +39,8 @@ #include #include "internal.h" =20 +int host_has_fsgsbase; + static void ptrace_child(void) { int ret; @@ -460,6 +464,20 @@ __uml_setup("seccomp=3D", uml_seccomp_config, " This is insecure and should only be used with a trusted userspace\n\n" ); =20 +static void __init check_fsgsbase(void) +{ + unsigned long auxv =3D getauxval(AT_HWCAP2); + + os_info("Checking FSGSBASE instructions..."); + if (auxv & HWCAP2_FSGSBASE) { + host_has_fsgsbase =3D 1; + os_info("OK\n"); + } else { + host_has_fsgsbase =3D 0; + os_info("disabled\n"); + } +} + void __init os_early_checks(void) { int pid; @@ -488,6 +506,9 @@ void __init os_early_checks(void) using_seccomp =3D 0; check_ptrace(); =20 + /* probe fsgsbase instruction */ + check_fsgsbase(); + pid =3D start_ptraced_child(); if (init_pid_registers(pid)) fatal("Failed to initialize default registers"); diff --git a/arch/x86/um/nommu/do_syscall_64.c b/arch/x86/um/nommu/do_sysca= ll_64.c index 292d7c578622..c08ae4e0edec 100644 --- a/arch/x86/um/nommu/do_syscall_64.c +++ b/arch/x86/um/nommu/do_syscall_64.c @@ -4,7 +4,9 @@ #include #include #include +#include #include +#include "syscalls.h" =20 __visible void do_syscall_64(struct pt_regs *regs) { @@ -13,6 +15,9 @@ __visible void do_syscall_64(struct pt_regs *regs) syscall =3D PT_SYSCALL_NR(regs->regs.gp); UPT_SYSCALL_NR(®s->regs) =3D syscall; =20 + /* set fs register to the original host one */ + os_x86_arch_prctl(0, ARCH_SET_FS, (void *)host_fs); + if (likely(syscall < NR_syscalls)) { unsigned long ret; =20 @@ -29,4 +34,10 @@ __visible void do_syscall_64(struct pt_regs *regs) =20 /* handle tasks and signals at the end */ interrupt_end(); + + /* restore back fs register to userspace configured one */ + os_x86_arch_prctl(0, ARCH_SET_FS, + (void *)(current->thread.regs.regs.gp[FS_BASE + / sizeof(unsigned long)])); + } diff --git a/arch/x86/um/nommu/syscalls.h b/arch/x86/um/nommu/syscalls.h index ce16bf8abd59..4b9e3c85cf3a 100644 --- a/arch/x86/um/nommu/syscalls.h +++ b/arch/x86/um/nommu/syscalls.h @@ -13,6 +13,7 @@ extern long current_top_of_stack; extern long current_ptregs; =20 +int os_x86_arch_prctl(int pid, int option, unsigned long *arg2); void arch_set_stack_to_current(void); =20 #endif diff --git a/arch/x86/um/nommu/syscalls_64.c b/arch/x86/um/nommu/syscalls_6= 4.c index 3b1610280110..25fb11a8b7a4 100644 --- a/arch/x86/um/nommu/syscalls_64.c +++ b/arch/x86/um/nommu/syscalls_64.c @@ -10,11 +10,48 @@ #include #include #include +#include #include /* XXX This should get the constants from libc */ #include #include #include "syscalls.h" =20 +/* + * The guest libc can change FS, which confuses the host libc. + * In fact, changing FS directly is not supported (check + * man arch_prctl). So, whenever we make a host syscall, + * we should be changing FS to the original FS (not the + * one set by the guest libc). This original FS is stored + * in host_fs. + */ +long long host_fs =3D -1; + +int os_x86_arch_prctl(int pid, int option, unsigned long *arg2) +{ + if (!host_has_fsgsbase) + return os_arch_prctl(pid, option, arg2); + + switch (option) { + case ARCH_SET_FS: + wrfsbase(*arg2); + break; + case ARCH_SET_GS: + wrgsbase(*arg2); + break; + case ARCH_GET_FS: + *arg2 =3D rdfsbase(); + break; + case ARCH_GET_GS: + *arg2 =3D rdgsbase(); + break; + default: + pr_warn("%s: unsupported option: 0x%x", __func__, option); + break; + } + + return 0; +} + void arch_set_stack_to_current(void) { current_top_of_stack =3D task_top_of_stack(current); @@ -35,6 +72,15 @@ void arch_switch_to(struct task_struct *to) return; =20 /* this changes the FS on every context switch */ - arch_prctl(to, ARCH_SET_FS, + os_x86_arch_prctl(0, ARCH_SET_FS, (void __user *) to->thread.regs.regs.gp[FS_BASE / sizeof(unsigned lon= g)]); } + +static int __init um_nommu_setup_hostfs(void) +{ + /* initialize the host_fs value at boottime */ + os_x86_arch_prctl(0, ARCH_GET_FS, (void *)&host_fs); + + return 0; +} +arch_initcall(um_nommu_setup_hostfs); --=20 2.43.0