xref: /linux/arch/um/kernel/skas/mmu.c (revision 827634added7f38b7d724cab1dccdb2b004c13c3)
1 /*
2  * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3  * Licensed under the GPL
4  */
5 
6 #include <linux/mm.h>
7 #include <linux/sched.h>
8 #include <linux/slab.h>
9 #include <asm/pgalloc.h>
10 #include <asm/pgtable.h>
11 #include <as-layout.h>
12 #include <os.h>
13 #include <skas.h>
14 
15 extern int __syscall_stub_start;
16 
17 static int init_stub_pte(struct mm_struct *mm, unsigned long proc,
18 			 unsigned long kernel)
19 {
20 	pgd_t *pgd;
21 	pud_t *pud;
22 	pmd_t *pmd;
23 	pte_t *pte;
24 
25 	pgd = pgd_offset(mm, proc);
26 	pud = pud_alloc(mm, pgd, proc);
27 	if (!pud)
28 		goto out;
29 
30 	pmd = pmd_alloc(mm, pud, proc);
31 	if (!pmd)
32 		goto out_pmd;
33 
34 	pte = pte_alloc_map(mm, NULL, pmd, proc);
35 	if (!pte)
36 		goto out_pte;
37 
38 	*pte = mk_pte(virt_to_page(kernel), __pgprot(_PAGE_PRESENT));
39 	*pte = pte_mkread(*pte);
40 	return 0;
41 
42  out_pte:
43 	pmd_free(mm, pmd);
44  out_pmd:
45 	pud_free(mm, pud);
46  out:
47 	return -ENOMEM;
48 }
49 
50 int init_new_context(struct task_struct *task, struct mm_struct *mm)
51 {
52  	struct mm_context *from_mm = NULL;
53 	struct mm_context *to_mm = &mm->context;
54 	unsigned long stack = 0;
55 	int ret = -ENOMEM;
56 
57 	stack = get_zeroed_page(GFP_KERNEL);
58 	if (stack == 0)
59 		goto out;
60 
61 	to_mm->id.stack = stack;
62 	if (current->mm != NULL && current->mm != &init_mm)
63 		from_mm = &current->mm->context;
64 
65 	if (from_mm)
66 		to_mm->id.u.pid = copy_context_skas0(stack,
67 						     from_mm->id.u.pid);
68 	else to_mm->id.u.pid = start_userspace(stack);
69 
70 	if (to_mm->id.u.pid < 0) {
71 		ret = to_mm->id.u.pid;
72 		goto out_free;
73 	}
74 
75 	ret = init_new_ldt(to_mm, from_mm);
76 	if (ret < 0) {
77 		printk(KERN_ERR "init_new_context_skas - init_ldt"
78 		       " failed, errno = %d\n", ret);
79 		goto out_free;
80 	}
81 
82 	return 0;
83 
84  out_free:
85 	if (to_mm->id.stack != 0)
86 		free_page(to_mm->id.stack);
87  out:
88 	return ret;
89 }
90 
91 void uml_setup_stubs(struct mm_struct *mm)
92 {
93 	int err, ret;
94 
95 	ret = init_stub_pte(mm, STUB_CODE,
96 			    (unsigned long) &__syscall_stub_start);
97 	if (ret)
98 		goto out;
99 
100 	ret = init_stub_pte(mm, STUB_DATA, mm->context.id.stack);
101 	if (ret)
102 		goto out;
103 
104 	mm->context.stub_pages[0] = virt_to_page(&__syscall_stub_start);
105 	mm->context.stub_pages[1] = virt_to_page(mm->context.id.stack);
106 
107 	/* dup_mmap already holds mmap_sem */
108 	err = install_special_mapping(mm, STUB_START, STUB_END - STUB_START,
109 				      VM_READ | VM_MAYREAD | VM_EXEC |
110 				      VM_MAYEXEC | VM_DONTCOPY | VM_PFNMAP,
111 				      mm->context.stub_pages);
112 	if (err) {
113 		printk(KERN_ERR "install_special_mapping returned %d\n", err);
114 		goto out;
115 	}
116 	return;
117 
118 out:
119 	force_sigsegv(SIGSEGV, current);
120 }
121 
122 void arch_exit_mmap(struct mm_struct *mm)
123 {
124 	pte_t *pte;
125 
126 	pte = virt_to_pte(mm, STUB_CODE);
127 	if (pte != NULL)
128 		pte_clear(mm, STUB_CODE, pte);
129 
130 	pte = virt_to_pte(mm, STUB_DATA);
131 	if (pte == NULL)
132 		return;
133 
134 	pte_clear(mm, STUB_DATA, pte);
135 }
136 
137 void destroy_context(struct mm_struct *mm)
138 {
139 	struct mm_context *mmu = &mm->context;
140 
141 	/*
142 	 * If init_new_context wasn't called, this will be
143 	 * zero, resulting in a kill(0), which will result in the
144 	 * whole UML suddenly dying.  Also, cover negative and
145 	 * 1 cases, since they shouldn't happen either.
146 	 */
147 	if (mmu->id.u.pid < 2) {
148 		printk(KERN_ERR "corrupt mm_context - pid = %d\n",
149 		       mmu->id.u.pid);
150 		return;
151 	}
152 	os_kill_ptraced_process(mmu->id.u.pid, 1);
153 
154 	free_page(mmu->id.stack);
155 	free_ldt(mmu);
156 }
157