xref: /OK3568_Linux_fs/kernel/arch/x86/entry/vsyscall/vsyscall_64.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2012-2014 Andy Lutomirski <luto@amacapital.net>
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Based on the original implementation which is:
6*4882a593Smuzhiyun  *  Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
7*4882a593Smuzhiyun  *  Copyright 2003 Andi Kleen, SuSE Labs.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  *  Parts of the original code have been moved to arch/x86/vdso/vma.c
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * This file implements vsyscall emulation.  vsyscalls are a legacy ABI:
12*4882a593Smuzhiyun  * Userspace can request certain kernel services by calling fixed
13*4882a593Smuzhiyun  * addresses.  This concept is problematic:
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * - It interferes with ASLR.
16*4882a593Smuzhiyun  * - It's awkward to write code that lives in kernel addresses but is
17*4882a593Smuzhiyun  *   callable by userspace at fixed addresses.
18*4882a593Smuzhiyun  * - The whole concept is impossible for 32-bit compat userspace.
19*4882a593Smuzhiyun  * - UML cannot easily virtualize a vsyscall.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * As of mid-2014, I believe that there is no new userspace code that
22*4882a593Smuzhiyun  * will use a vsyscall if the vDSO is present.  I hope that there will
23*4882a593Smuzhiyun  * soon be no new userspace code that will ever use a vsyscall.
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * The code in this file emulates vsyscalls when notified of a page
26*4882a593Smuzhiyun  * fault to a vsyscall address.
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #include <linux/kernel.h>
30*4882a593Smuzhiyun #include <linux/timer.h>
31*4882a593Smuzhiyun #include <linux/sched/signal.h>
32*4882a593Smuzhiyun #include <linux/mm_types.h>
33*4882a593Smuzhiyun #include <linux/syscalls.h>
34*4882a593Smuzhiyun #include <linux/ratelimit.h>
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #include <asm/vsyscall.h>
37*4882a593Smuzhiyun #include <asm/unistd.h>
38*4882a593Smuzhiyun #include <asm/fixmap.h>
39*4882a593Smuzhiyun #include <asm/traps.h>
40*4882a593Smuzhiyun #include <asm/paravirt.h>
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #define CREATE_TRACE_POINTS
43*4882a593Smuzhiyun #include "vsyscall_trace.h"
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun static enum { EMULATE, XONLY, NONE } vsyscall_mode __ro_after_init =
46*4882a593Smuzhiyun #ifdef CONFIG_LEGACY_VSYSCALL_NONE
47*4882a593Smuzhiyun 	NONE;
48*4882a593Smuzhiyun #elif defined(CONFIG_LEGACY_VSYSCALL_XONLY)
49*4882a593Smuzhiyun 	XONLY;
50*4882a593Smuzhiyun #else
51*4882a593Smuzhiyun 	EMULATE;
52*4882a593Smuzhiyun #endif
53*4882a593Smuzhiyun 
vsyscall_setup(char * str)54*4882a593Smuzhiyun static int __init vsyscall_setup(char *str)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	if (str) {
57*4882a593Smuzhiyun 		if (!strcmp("emulate", str))
58*4882a593Smuzhiyun 			vsyscall_mode = EMULATE;
59*4882a593Smuzhiyun 		else if (!strcmp("xonly", str))
60*4882a593Smuzhiyun 			vsyscall_mode = XONLY;
61*4882a593Smuzhiyun 		else if (!strcmp("none", str))
62*4882a593Smuzhiyun 			vsyscall_mode = NONE;
63*4882a593Smuzhiyun 		else
64*4882a593Smuzhiyun 			return -EINVAL;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 		return 0;
67*4882a593Smuzhiyun 	}
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	return -EINVAL;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun early_param("vsyscall", vsyscall_setup);
72*4882a593Smuzhiyun 
warn_bad_vsyscall(const char * level,struct pt_regs * regs,const char * message)73*4882a593Smuzhiyun static void warn_bad_vsyscall(const char *level, struct pt_regs *regs,
74*4882a593Smuzhiyun 			      const char *message)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	if (!show_unhandled_signals)
77*4882a593Smuzhiyun 		return;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	printk_ratelimited("%s%s[%d] %s ip:%lx cs:%lx sp:%lx ax:%lx si:%lx di:%lx\n",
80*4882a593Smuzhiyun 			   level, current->comm, task_pid_nr(current),
81*4882a593Smuzhiyun 			   message, regs->ip, regs->cs,
82*4882a593Smuzhiyun 			   regs->sp, regs->ax, regs->si, regs->di);
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
addr_to_vsyscall_nr(unsigned long addr)85*4882a593Smuzhiyun static int addr_to_vsyscall_nr(unsigned long addr)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	int nr;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	if ((addr & ~0xC00UL) != VSYSCALL_ADDR)
90*4882a593Smuzhiyun 		return -EINVAL;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	nr = (addr & 0xC00UL) >> 10;
93*4882a593Smuzhiyun 	if (nr >= 3)
94*4882a593Smuzhiyun 		return -EINVAL;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	return nr;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
write_ok_or_segv(unsigned long ptr,size_t size)99*4882a593Smuzhiyun static bool write_ok_or_segv(unsigned long ptr, size_t size)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	/*
102*4882a593Smuzhiyun 	 * XXX: if access_ok, get_user, and put_user handled
103*4882a593Smuzhiyun 	 * sig_on_uaccess_err, this could go away.
104*4882a593Smuzhiyun 	 */
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	if (!access_ok((void __user *)ptr, size)) {
107*4882a593Smuzhiyun 		struct thread_struct *thread = &current->thread;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 		thread->error_code	= X86_PF_USER | X86_PF_WRITE;
110*4882a593Smuzhiyun 		thread->cr2		= ptr;
111*4882a593Smuzhiyun 		thread->trap_nr		= X86_TRAP_PF;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 		force_sig_fault(SIGSEGV, SEGV_MAPERR, (void __user *)ptr);
114*4882a593Smuzhiyun 		return false;
115*4882a593Smuzhiyun 	} else {
116*4882a593Smuzhiyun 		return true;
117*4882a593Smuzhiyun 	}
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
emulate_vsyscall(unsigned long error_code,struct pt_regs * regs,unsigned long address)120*4882a593Smuzhiyun bool emulate_vsyscall(unsigned long error_code,
121*4882a593Smuzhiyun 		      struct pt_regs *regs, unsigned long address)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	struct task_struct *tsk;
124*4882a593Smuzhiyun 	unsigned long caller;
125*4882a593Smuzhiyun 	int vsyscall_nr, syscall_nr, tmp;
126*4882a593Smuzhiyun 	int prev_sig_on_uaccess_err;
127*4882a593Smuzhiyun 	long ret;
128*4882a593Smuzhiyun 	unsigned long orig_dx;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	/* Write faults or kernel-privilege faults never get fixed up. */
131*4882a593Smuzhiyun 	if ((error_code & (X86_PF_WRITE | X86_PF_USER)) != X86_PF_USER)
132*4882a593Smuzhiyun 		return false;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	if (!(error_code & X86_PF_INSTR)) {
135*4882a593Smuzhiyun 		/* Failed vsyscall read */
136*4882a593Smuzhiyun 		if (vsyscall_mode == EMULATE)
137*4882a593Smuzhiyun 			return false;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 		/*
140*4882a593Smuzhiyun 		 * User code tried and failed to read the vsyscall page.
141*4882a593Smuzhiyun 		 */
142*4882a593Smuzhiyun 		warn_bad_vsyscall(KERN_INFO, regs, "vsyscall read attempt denied -- look up the vsyscall kernel parameter if you need a workaround");
143*4882a593Smuzhiyun 		return false;
144*4882a593Smuzhiyun 	}
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	/*
147*4882a593Smuzhiyun 	 * No point in checking CS -- the only way to get here is a user mode
148*4882a593Smuzhiyun 	 * trap to a high address, which means that we're in 64-bit user code.
149*4882a593Smuzhiyun 	 */
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	WARN_ON_ONCE(address != regs->ip);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	if (vsyscall_mode == NONE) {
154*4882a593Smuzhiyun 		warn_bad_vsyscall(KERN_INFO, regs,
155*4882a593Smuzhiyun 				  "vsyscall attempted with vsyscall=none");
156*4882a593Smuzhiyun 		return false;
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	vsyscall_nr = addr_to_vsyscall_nr(address);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	trace_emulate_vsyscall(vsyscall_nr);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	if (vsyscall_nr < 0) {
164*4882a593Smuzhiyun 		warn_bad_vsyscall(KERN_WARNING, regs,
165*4882a593Smuzhiyun 				  "misaligned vsyscall (exploit attempt or buggy program) -- look up the vsyscall kernel parameter if you need a workaround");
166*4882a593Smuzhiyun 		goto sigsegv;
167*4882a593Smuzhiyun 	}
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	if (get_user(caller, (unsigned long __user *)regs->sp) != 0) {
170*4882a593Smuzhiyun 		warn_bad_vsyscall(KERN_WARNING, regs,
171*4882a593Smuzhiyun 				  "vsyscall with bad stack (exploit attempt?)");
172*4882a593Smuzhiyun 		goto sigsegv;
173*4882a593Smuzhiyun 	}
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	tsk = current;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	/*
178*4882a593Smuzhiyun 	 * Check for access_ok violations and find the syscall nr.
179*4882a593Smuzhiyun 	 *
180*4882a593Smuzhiyun 	 * NULL is a valid user pointer (in the access_ok sense) on 32-bit and
181*4882a593Smuzhiyun 	 * 64-bit, so we don't need to special-case it here.  For all the
182*4882a593Smuzhiyun 	 * vsyscalls, NULL means "don't write anything" not "write it at
183*4882a593Smuzhiyun 	 * address 0".
184*4882a593Smuzhiyun 	 */
185*4882a593Smuzhiyun 	switch (vsyscall_nr) {
186*4882a593Smuzhiyun 	case 0:
187*4882a593Smuzhiyun 		if (!write_ok_or_segv(regs->di, sizeof(struct __kernel_old_timeval)) ||
188*4882a593Smuzhiyun 		    !write_ok_or_segv(regs->si, sizeof(struct timezone))) {
189*4882a593Smuzhiyun 			ret = -EFAULT;
190*4882a593Smuzhiyun 			goto check_fault;
191*4882a593Smuzhiyun 		}
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 		syscall_nr = __NR_gettimeofday;
194*4882a593Smuzhiyun 		break;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	case 1:
197*4882a593Smuzhiyun 		if (!write_ok_or_segv(regs->di, sizeof(__kernel_old_time_t))) {
198*4882a593Smuzhiyun 			ret = -EFAULT;
199*4882a593Smuzhiyun 			goto check_fault;
200*4882a593Smuzhiyun 		}
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 		syscall_nr = __NR_time;
203*4882a593Smuzhiyun 		break;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	case 2:
206*4882a593Smuzhiyun 		if (!write_ok_or_segv(regs->di, sizeof(unsigned)) ||
207*4882a593Smuzhiyun 		    !write_ok_or_segv(regs->si, sizeof(unsigned))) {
208*4882a593Smuzhiyun 			ret = -EFAULT;
209*4882a593Smuzhiyun 			goto check_fault;
210*4882a593Smuzhiyun 		}
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 		syscall_nr = __NR_getcpu;
213*4882a593Smuzhiyun 		break;
214*4882a593Smuzhiyun 	}
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	/*
217*4882a593Smuzhiyun 	 * Handle seccomp.  regs->ip must be the original value.
218*4882a593Smuzhiyun 	 * See seccomp_send_sigsys and Documentation/userspace-api/seccomp_filter.rst.
219*4882a593Smuzhiyun 	 *
220*4882a593Smuzhiyun 	 * We could optimize the seccomp disabled case, but performance
221*4882a593Smuzhiyun 	 * here doesn't matter.
222*4882a593Smuzhiyun 	 */
223*4882a593Smuzhiyun 	regs->orig_ax = syscall_nr;
224*4882a593Smuzhiyun 	regs->ax = -ENOSYS;
225*4882a593Smuzhiyun 	tmp = secure_computing();
226*4882a593Smuzhiyun 	if ((!tmp && regs->orig_ax != syscall_nr) || regs->ip != address) {
227*4882a593Smuzhiyun 		warn_bad_vsyscall(KERN_DEBUG, regs,
228*4882a593Smuzhiyun 				  "seccomp tried to change syscall nr or ip");
229*4882a593Smuzhiyun 		do_exit(SIGSYS);
230*4882a593Smuzhiyun 	}
231*4882a593Smuzhiyun 	regs->orig_ax = -1;
232*4882a593Smuzhiyun 	if (tmp)
233*4882a593Smuzhiyun 		goto do_ret;  /* skip requested */
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	/*
236*4882a593Smuzhiyun 	 * With a real vsyscall, page faults cause SIGSEGV.  We want to
237*4882a593Smuzhiyun 	 * preserve that behavior to make writing exploits harder.
238*4882a593Smuzhiyun 	 */
239*4882a593Smuzhiyun 	prev_sig_on_uaccess_err = current->thread.sig_on_uaccess_err;
240*4882a593Smuzhiyun 	current->thread.sig_on_uaccess_err = 1;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	ret = -EFAULT;
243*4882a593Smuzhiyun 	switch (vsyscall_nr) {
244*4882a593Smuzhiyun 	case 0:
245*4882a593Smuzhiyun 		/* this decodes regs->di and regs->si on its own */
246*4882a593Smuzhiyun 		ret = __x64_sys_gettimeofday(regs);
247*4882a593Smuzhiyun 		break;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	case 1:
250*4882a593Smuzhiyun 		/* this decodes regs->di on its own */
251*4882a593Smuzhiyun 		ret = __x64_sys_time(regs);
252*4882a593Smuzhiyun 		break;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	case 2:
255*4882a593Smuzhiyun 		/* while we could clobber regs->dx, we didn't in the past... */
256*4882a593Smuzhiyun 		orig_dx = regs->dx;
257*4882a593Smuzhiyun 		regs->dx = 0;
258*4882a593Smuzhiyun 		/* this decodes regs->di, regs->si and regs->dx on its own */
259*4882a593Smuzhiyun 		ret = __x64_sys_getcpu(regs);
260*4882a593Smuzhiyun 		regs->dx = orig_dx;
261*4882a593Smuzhiyun 		break;
262*4882a593Smuzhiyun 	}
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	current->thread.sig_on_uaccess_err = prev_sig_on_uaccess_err;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun check_fault:
267*4882a593Smuzhiyun 	if (ret == -EFAULT) {
268*4882a593Smuzhiyun 		/* Bad news -- userspace fed a bad pointer to a vsyscall. */
269*4882a593Smuzhiyun 		warn_bad_vsyscall(KERN_INFO, regs,
270*4882a593Smuzhiyun 				  "vsyscall fault (exploit attempt?)");
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 		/*
273*4882a593Smuzhiyun 		 * If we failed to generate a signal for any reason,
274*4882a593Smuzhiyun 		 * generate one here.  (This should be impossible.)
275*4882a593Smuzhiyun 		 */
276*4882a593Smuzhiyun 		if (WARN_ON_ONCE(!sigismember(&tsk->pending.signal, SIGBUS) &&
277*4882a593Smuzhiyun 				 !sigismember(&tsk->pending.signal, SIGSEGV)))
278*4882a593Smuzhiyun 			goto sigsegv;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 		return true;  /* Don't emulate the ret. */
281*4882a593Smuzhiyun 	}
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	regs->ax = ret;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun do_ret:
286*4882a593Smuzhiyun 	/* Emulate a ret instruction. */
287*4882a593Smuzhiyun 	regs->ip = caller;
288*4882a593Smuzhiyun 	regs->sp += 8;
289*4882a593Smuzhiyun 	return true;
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun sigsegv:
292*4882a593Smuzhiyun 	force_sig(SIGSEGV);
293*4882a593Smuzhiyun 	return true;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun /*
297*4882a593Smuzhiyun  * A pseudo VMA to allow ptrace access for the vsyscall page.  This only
298*4882a593Smuzhiyun  * covers the 64bit vsyscall page now. 32bit has a real VMA now and does
299*4882a593Smuzhiyun  * not need special handling anymore:
300*4882a593Smuzhiyun  */
gate_vma_name(struct vm_area_struct * vma)301*4882a593Smuzhiyun static const char *gate_vma_name(struct vm_area_struct *vma)
302*4882a593Smuzhiyun {
303*4882a593Smuzhiyun 	return "[vsyscall]";
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun static const struct vm_operations_struct gate_vma_ops = {
306*4882a593Smuzhiyun 	.name = gate_vma_name,
307*4882a593Smuzhiyun };
308*4882a593Smuzhiyun static struct vm_area_struct gate_vma __ro_after_init = {
309*4882a593Smuzhiyun 	.vm_start	= VSYSCALL_ADDR,
310*4882a593Smuzhiyun 	.vm_end		= VSYSCALL_ADDR + PAGE_SIZE,
311*4882a593Smuzhiyun 	.vm_page_prot	= PAGE_READONLY_EXEC,
312*4882a593Smuzhiyun 	.vm_flags	= VM_READ | VM_EXEC,
313*4882a593Smuzhiyun 	.vm_ops		= &gate_vma_ops,
314*4882a593Smuzhiyun };
315*4882a593Smuzhiyun 
get_gate_vma(struct mm_struct * mm)316*4882a593Smuzhiyun struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
319*4882a593Smuzhiyun 	if (!mm || mm->context.ia32_compat)
320*4882a593Smuzhiyun 		return NULL;
321*4882a593Smuzhiyun #endif
322*4882a593Smuzhiyun 	if (vsyscall_mode == NONE)
323*4882a593Smuzhiyun 		return NULL;
324*4882a593Smuzhiyun 	return &gate_vma;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun 
in_gate_area(struct mm_struct * mm,unsigned long addr)327*4882a593Smuzhiyun int in_gate_area(struct mm_struct *mm, unsigned long addr)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun 	struct vm_area_struct *vma = get_gate_vma(mm);
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	if (!vma)
332*4882a593Smuzhiyun 		return 0;
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	return (addr >= vma->vm_start) && (addr < vma->vm_end);
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun /*
338*4882a593Smuzhiyun  * Use this when you have no reliable mm, typically from interrupt
339*4882a593Smuzhiyun  * context. It is less reliable than using a task's mm and may give
340*4882a593Smuzhiyun  * false positives.
341*4882a593Smuzhiyun  */
in_gate_area_no_mm(unsigned long addr)342*4882a593Smuzhiyun int in_gate_area_no_mm(unsigned long addr)
343*4882a593Smuzhiyun {
344*4882a593Smuzhiyun 	return vsyscall_mode != NONE && (addr & PAGE_MASK) == VSYSCALL_ADDR;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun /*
348*4882a593Smuzhiyun  * The VSYSCALL page is the only user-accessible page in the kernel address
349*4882a593Smuzhiyun  * range.  Normally, the kernel page tables can have _PAGE_USER clear, but
350*4882a593Smuzhiyun  * the tables covering VSYSCALL_ADDR need _PAGE_USER set if vsyscalls
351*4882a593Smuzhiyun  * are enabled.
352*4882a593Smuzhiyun  *
353*4882a593Smuzhiyun  * Some day we may create a "minimal" vsyscall mode in which we emulate
354*4882a593Smuzhiyun  * vsyscalls but leave the page not present.  If so, we skip calling
355*4882a593Smuzhiyun  * this.
356*4882a593Smuzhiyun  */
set_vsyscall_pgtable_user_bits(pgd_t * root)357*4882a593Smuzhiyun void __init set_vsyscall_pgtable_user_bits(pgd_t *root)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun 	pgd_t *pgd;
360*4882a593Smuzhiyun 	p4d_t *p4d;
361*4882a593Smuzhiyun 	pud_t *pud;
362*4882a593Smuzhiyun 	pmd_t *pmd;
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	pgd = pgd_offset_pgd(root, VSYSCALL_ADDR);
365*4882a593Smuzhiyun 	set_pgd(pgd, __pgd(pgd_val(*pgd) | _PAGE_USER));
366*4882a593Smuzhiyun 	p4d = p4d_offset(pgd, VSYSCALL_ADDR);
367*4882a593Smuzhiyun #if CONFIG_PGTABLE_LEVELS >= 5
368*4882a593Smuzhiyun 	set_p4d(p4d, __p4d(p4d_val(*p4d) | _PAGE_USER));
369*4882a593Smuzhiyun #endif
370*4882a593Smuzhiyun 	pud = pud_offset(p4d, VSYSCALL_ADDR);
371*4882a593Smuzhiyun 	set_pud(pud, __pud(pud_val(*pud) | _PAGE_USER));
372*4882a593Smuzhiyun 	pmd = pmd_offset(pud, VSYSCALL_ADDR);
373*4882a593Smuzhiyun 	set_pmd(pmd, __pmd(pmd_val(*pmd) | _PAGE_USER));
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun 
map_vsyscall(void)376*4882a593Smuzhiyun void __init map_vsyscall(void)
377*4882a593Smuzhiyun {
378*4882a593Smuzhiyun 	extern char __vsyscall_page;
379*4882a593Smuzhiyun 	unsigned long physaddr_vsyscall = __pa_symbol(&__vsyscall_page);
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	/*
382*4882a593Smuzhiyun 	 * For full emulation, the page needs to exist for real.  In
383*4882a593Smuzhiyun 	 * execute-only mode, there is no PTE at all backing the vsyscall
384*4882a593Smuzhiyun 	 * page.
385*4882a593Smuzhiyun 	 */
386*4882a593Smuzhiyun 	if (vsyscall_mode == EMULATE) {
387*4882a593Smuzhiyun 		__set_fixmap(VSYSCALL_PAGE, physaddr_vsyscall,
388*4882a593Smuzhiyun 			     PAGE_KERNEL_VVAR);
389*4882a593Smuzhiyun 		set_vsyscall_pgtable_user_bits(swapper_pg_dir);
390*4882a593Smuzhiyun 	}
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	if (vsyscall_mode == XONLY)
393*4882a593Smuzhiyun 		gate_vma.vm_flags = VM_EXEC;
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	BUILD_BUG_ON((unsigned long)__fix_to_virt(VSYSCALL_PAGE) !=
396*4882a593Smuzhiyun 		     (unsigned long)VSYSCALL_ADDR);
397*4882a593Smuzhiyun }
398