bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RFC PATCH gnumach] percpu area using gs segment


From: Luca
Subject: Re: [RFC PATCH gnumach] percpu area using gs segment
Date: Sun, 27 Aug 2023 12:08:44 +0200

Hi,

Il 26/08/23 08:48, Damien Zammit ha scritto:
diff --git a/i386/i386/cpu_number.c b/i386/i386/cpu_number.c
index ef19e11f..241015b5 100644
--- a/i386/i386/cpu_number.c
+++ b/i386/i386/cpu_number.c
@@ -20,11 +20,17 @@
  #include <i386/smp.h>
  #include <i386/cpu.h>
  #include <i386/mp_desc.h>
+#include <i386/percpu.h>
  #include <kern/printf.h>
#if NCPUS > 1
-int cpu_number(void)
+int cpu_number_slow(void)
  {
        return cpu_id_lut[apic_get_current_cpu()];
  }

This should also be defined for non-smp, otherwise compilation fails (I only tested x86_64). Also I would make compilaiton explicitely fail somewhere on x86_64+smp for now.

diff --git a/i386/i386/percpu.c b/i386/i386/percpu.c
new file mode 100644
index 00000000..0bc8b234
--- /dev/null
+++ b/i386/i386/percpu.c
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2023 Free Software Foundation, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <i386/smp.h>
+#include <i386/apic.h>
+#include <i386/percpu.h>
+
+struct percpu percpu_array[NCPUS] __aligned(0x8000);

Why do you need this alignment?


diff --git a/i386/i386/percpu.h b/i386/i386/percpu.h
new file mode 100644
index 00000000..b22d512c
--- /dev/null
+++ b/i386/i386/percpu.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2023 Free Software Foundation, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _PERCPU_H_
+#define _PERCPU_H_
+
+#include <kern/ast.h>
+#include <kern/processor.h>
+#include <kern/thread.h>
+#include <kern/timer.h>
+#include <i386/mp_desc.h>
+#include <i386/spl.h>
+#include <intel/pmap.h>
+#include <ipc/ipc_kmsg.h>
+
+#define percpu_assign(stm, val)     \
+    asm("mov %0, %%gs:%1"           \
+                 : : "r" (val), "m" (__builtin_offsetof(struct percpu, stm)));
+
+#define percpu_ptr(typ, stm)        \
+MACRO_BEGIN                         \
+    typ *ptr_ = (typ *)__builtin_offsetof(struct percpu, stm); \
+                                    \
+    asm("add %%gs:0, %0"            \
+                 : "+r" (ptr_)      \
+                 : );               \
+                                    \
+    ptr_;                           \
+MACRO_END

If it could simplify this accessor, gcc (and also clang, if we ever need it) supports using a specific segment base for a variable:

https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gcc/Named-Address-Spaces.html#x86-Named-Address-Spaces

and

https://kernel.org/doc/html/next/x86/x86_64/fsgs.html#compiler-support-for-fs-gs-based-addressing


Luca



reply via email to

[Prev in Thread] Current Thread [Next in Thread]