From: Zachary Amsden <zach@vmware.com>

These changes allow a sub-architecture to change the notion of privilege by
running the kernel at CPL 0, 1, or 2.  The make_kernel_segment() macro can be
redefined by a subarchitecture to change the RPL on kernel segments to the
appropriate value, and the tests user_mode() and user_mode_vm() may be
similarly overridden.

Changes to the assembly code are required to fully support this, and provided
in a separate patch.

Signed-off-by: Zachary Amsden <zach@vmware.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
---

 arch/i386/kernel/process.c                   |    2 -
 arch/i386/kernel/traps.c                     |    4 +--
 include/asm-i386/mach-default/mach_segment.h |   28 +++++++++++++++++++++++++++
 include/asm-i386/ptrace.h                    |   16 ---------------
 4 files changed, 32 insertions(+), 18 deletions(-)

diff -puN arch/i386/kernel/process.c~i386-transparent-paravirtualization-sub-arch-create-accessors-that-allow-the-i386-kernel-to-run-at arch/i386/kernel/process.c
--- devel/arch/i386/kernel/process.c~i386-transparent-paravirtualization-sub-arch-create-accessors-that-allow-the-i386-kernel-to-run-at	2005-08-06 15:01:42.000000000 -0700
+++ devel-akpm/arch/i386/kernel/process.c	2005-08-06 15:01:42.000000000 -0700
@@ -355,7 +355,7 @@ int kernel_thread(int (*fn)(void *), voi
 	regs.xes = __USER_DS;
 	regs.orig_eax = -1;
 	regs.eip = (unsigned long) kernel_thread_helper;
-	regs.xcs = __KERNEL_CS;
+	regs.xcs = make_kernel_segment(__KERNEL_CS);
 	regs.eflags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
 
 	/* Ok, create the new process.. */
diff -puN arch/i386/kernel/traps.c~i386-transparent-paravirtualization-sub-arch-create-accessors-that-allow-the-i386-kernel-to-run-at arch/i386/kernel/traps.c
--- devel/arch/i386/kernel/traps.c~i386-transparent-paravirtualization-sub-arch-create-accessors-that-allow-the-i386-kernel-to-run-at	2005-08-06 15:01:42.000000000 -0700
+++ devel-akpm/arch/i386/kernel/traps.c	2005-08-06 15:01:42.000000000 -0700
@@ -938,10 +938,10 @@ fastcall void setup_x86_bogus_stack(unsi
 	memcpy((void *)(stack_bot + iret_frame16_off), &regs->eip, 20);
 	/* fill in the switch pointers */
 	switch16_ptr[0] = (regs->esp & 0xffff0000) | iret_frame16_off;
-	switch16_ptr[1] = __ESPFIX_SS;
+	switch16_ptr[1] = make_kernel_segment(__ESPFIX_SS);
 	switch32_ptr[0] = (unsigned long)stk + sizeof(struct pt_regs) +
 		8 - CPU_16BIT_STACK_SIZE;
-	switch32_ptr[1] = __KERNEL_DS;
+	switch32_ptr[1] = make_kernel_segment(__KERNEL_DS);
 }
 
 fastcall unsigned char * fixup_x86_bogus_stack(unsigned short sp)
diff -puN include/asm-i386/mach-default/mach_segment.h~i386-transparent-paravirtualization-sub-arch-create-accessors-that-allow-the-i386-kernel-to-run-at include/asm-i386/mach-default/mach_segment.h
--- devel/include/asm-i386/mach-default/mach_segment.h~i386-transparent-paravirtualization-sub-arch-create-accessors-that-allow-the-i386-kernel-to-run-at	2005-08-06 15:01:42.000000000 -0700
+++ devel-akpm/include/asm-i386/mach-default/mach_segment.h	2005-08-06 15:01:42.000000000 -0700
@@ -0,0 +1,28 @@
+/*
+ * include/asm-i386/mach-default/mach_segment.h
+ *
+ * user_mode macros moved from include/asm-i386/ptrace.h 08/05
+ */
+
+#ifndef __MACH_SEGMENT_H
+#define __MACH_SEGMENT_H
+
+/*
+ * user_mode_vm(regs) determines whether a register set came from user mode.
+ * This is true if V8086 mode was enabled OR if the register set was from
+ * protected mode with RPL-3 CS value.  This tricky test checks that with
+ * one comparison.  Many places in the kernel can bypass this full check
+ * if they have already ruled out V8086 mode, so user_mode(regs) can be used.
+ */
+static inline int user_mode(struct pt_regs *regs)
+{
+	return (regs->xcs & 3) != 0;
+}
+static inline int user_mode_vm(struct pt_regs *regs)
+{
+	return ((regs->xcs & 3) | (regs->eflags & VM_MASK)) != 0;
+}
+
+#define make_kernel_segment(seg)	(seg)
+
+#endif
diff -puN include/asm-i386/ptrace.h~i386-transparent-paravirtualization-sub-arch-create-accessors-that-allow-the-i386-kernel-to-run-at include/asm-i386/ptrace.h
--- devel/include/asm-i386/ptrace.h~i386-transparent-paravirtualization-sub-arch-create-accessors-that-allow-the-i386-kernel-to-run-at	2005-08-06 15:01:42.000000000 -0700
+++ devel-akpm/include/asm-i386/ptrace.h	2005-08-06 15:01:42.000000000 -0700
@@ -57,25 +57,11 @@ struct pt_regs {
 #ifdef __KERNEL__
 
 #include <asm/vm86.h>
+#include <mach_segment.h>
 
 struct task_struct;
 extern void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code);
 
-/*
- * user_mode_vm(regs) determines whether a register set came from user mode.
- * This is true if V8086 mode was enabled OR if the register set was from
- * protected mode with RPL-3 CS value.  This tricky test checks that with
- * one comparison.  Many places in the kernel can bypass this full check
- * if they have already ruled out V8086 mode, so user_mode(regs) can be used.
- */
-static inline int user_mode(struct pt_regs *regs)
-{
-	return (regs->xcs & 3) != 0;
-}
-static inline int user_mode_vm(struct pt_regs *regs)
-{
-	return ((regs->xcs & 3) | (regs->eflags & VM_MASK)) != 0;
-}
 #define instruction_pointer(regs) ((regs)->eip)
 #if defined(CONFIG_SMP) && defined(CONFIG_FRAME_POINTER)
 extern unsigned long profile_pc(struct pt_regs *regs);
_