2 * Handling of different ABIs (personalities).
4 * We group personalities into execution domains which have their
5 * own handlers for kernel entry points, signal mapping, etc...
7 * 2001-05-06 Complete rewrite, Christoph Hellwig (hch@infradead.org)
10 #include <linux/config.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/kmod.h>
14 #include <linux/module.h>
15 #include <linux/personality.h>
16 #include <linux/sched.h>
17 #include <linux/sysctl.h>
18 #include <linux/types.h>
21 static void default_handler(int, struct pt_regs *);
23 static struct exec_domain *exec_domains = &default_exec_domain;
24 static rwlock_t exec_domains_lock = RW_LOCK_UNLOCKED;
27 static u_long ident_map[32] = {
28 0, 1, 2, 3, 4, 5, 6, 7,
29 8, 9, 10, 11, 12, 13, 14, 15,
30 16, 17, 18, 19, 20, 21, 22, 23,
31 24, 25, 26, 27, 28, 29, 30, 31
34 struct exec_domain default_exec_domain = {
35 .name = "Linux", /* name */
36 .handler = default_handler, /* lcall7 causes a seg fault. */
37 .pers_low = 0, /* PER_LINUX personality. */
38 .pers_high = 0, /* PER_LINUX personality. */
39 .signal_map = ident_map, /* Identity map signals. */
40 .signal_invmap = ident_map, /* - both ways. */
45 default_handler(int segment, struct pt_regs *regp)
50 * This may have been a static linked SVr4 binary, so we would
51 * have the personality set incorrectly. Or it might have been
52 * a Solaris/x86 binary. We can tell which because the former
53 * uses lcall7, while the latter used lcall 0x27.
54 * Try to find or load the appropriate personality, and fall back
55 * to just forcing a SEGV.
57 * XXX: this is IA32-specific and should be moved to the MD-tree.
62 pers = abi_defhandler_lcall7;
69 set_personality(pers);
71 if (current_thread_info()->exec_domain->handler != default_handler)
72 current_thread_info()->exec_domain->handler(segment, regp);
74 send_sig(SIGSEGV, current, 1);
77 static struct exec_domain *
78 lookup_exec_domain(u_long personality)
80 struct exec_domain * ep;
81 u_long pers = personality(personality);
83 read_lock(&exec_domains_lock);
84 for (ep = exec_domains; ep; ep = ep->next) {
85 if (pers >= ep->pers_low && pers <= ep->pers_high)
86 if (try_module_get(ep->module))
91 read_unlock(&exec_domains_lock);
92 request_module("personality-%ld", pers);
93 read_lock(&exec_domains_lock);
95 for (ep = exec_domains; ep; ep = ep->next) {
96 if (pers >= ep->pers_low && pers <= ep->pers_high)
97 if (try_module_get(ep->module))
102 ep = &default_exec_domain;
104 read_unlock(&exec_domains_lock);
109 register_exec_domain(struct exec_domain *ep)
111 struct exec_domain *tmp;
117 if (ep->next != NULL)
120 write_lock(&exec_domains_lock);
121 for (tmp = exec_domains; tmp; tmp = tmp->next) {
126 ep->next = exec_domains;
131 write_unlock(&exec_domains_lock);
136 unregister_exec_domain(struct exec_domain *ep)
138 struct exec_domain **epp;
141 write_lock(&exec_domains_lock);
142 for (epp = &exec_domains; *epp; epp = &(*epp)->next) {
146 write_unlock(&exec_domains_lock);
152 write_unlock(&exec_domains_lock);
157 __set_personality(u_long personality)
159 struct exec_domain *ep, *oep;
161 ep = lookup_exec_domain(personality);
162 if (ep == current_thread_info()->exec_domain) {
163 current->personality = personality;
167 if (atomic_read(¤t->fs->count) != 1) {
168 struct fs_struct *fsp, *ofsp;
170 fsp = copy_fs_struct(current->fs);
172 module_put(ep->module);
179 task_unlock(current);
185 * At that point we are guaranteed to be the sole owner of
189 current->personality = personality;
190 oep = current_thread_info()->exec_domain;
191 current_thread_info()->exec_domain = ep;
194 module_put(oep->module);
199 get_exec_domain_list(char *page)
201 struct exec_domain *ep;
204 read_lock(&exec_domains_lock);
205 for (ep = exec_domains; ep && len < PAGE_SIZE - 80; ep = ep->next)
206 len += sprintf(page + len, "%d-%d\t%-16s\t[%s]\n",
207 ep->pers_low, ep->pers_high, ep->name,
208 module_name(ep->module));
209 read_unlock(&exec_domains_lock);
214 sys_personality(u_long personality)
216 u_long old = current->personality;
218 if (personality != 0xffffffff) {
219 set_personality(personality);
220 if (current->personality != personality)
228 EXPORT_SYMBOL(register_exec_domain);
229 EXPORT_SYMBOL(unregister_exec_domain);
230 EXPORT_SYMBOL(__set_personality);
233 * We have to have all sysctl handling for the Linux-ABI
234 * in one place as the dynamic registration of sysctls is
235 * horribly crufty in Linux <= 2.4.
237 * I hope the new sysctl schemes discussed for future versions
238 * will obsolete this.
243 u_long abi_defhandler_coff = PER_SCOSVR3;
244 u_long abi_defhandler_elf = PER_LINUX;
245 u_long abi_defhandler_lcall7 = PER_SVR4;
246 u_long abi_defhandler_libcso = PER_SVR4;
248 int abi_fake_utsname;
250 static struct ctl_table abi_table[] = {
252 .ctl_name = ABI_DEFHANDLER_COFF,
253 .procname = "defhandler_coff",
254 .data = &abi_defhandler_coff,
255 .maxlen = sizeof(int),
257 .proc_handler = &proc_doulongvec_minmax,
260 .ctl_name = ABI_DEFHANDLER_ELF,
261 .procname = "defhandler_elf",
262 .data = &abi_defhandler_elf,
263 .maxlen = sizeof(int),
265 .proc_handler = &proc_doulongvec_minmax,
268 .ctl_name = ABI_DEFHANDLER_LCALL7,
269 .procname = "defhandler_lcall7",
270 .data = &abi_defhandler_lcall7,
271 .maxlen = sizeof(int),
273 .proc_handler = &proc_doulongvec_minmax,
276 .ctl_name = ABI_DEFHANDLER_LIBCSO,
277 .procname = "defhandler_libcso",
278 .data = &abi_defhandler_libcso,
279 .maxlen = sizeof(int),
281 .proc_handler = &proc_doulongvec_minmax,
284 .ctl_name = ABI_TRACE,
286 .data = &abi_traceflg,
287 .maxlen = sizeof(u_int),
289 .proc_handler = &proc_dointvec,
292 .ctl_name = ABI_FAKE_UTSNAME,
293 .procname = "fake_utsname",
294 .data = &abi_fake_utsname,
295 .maxlen = sizeof(int),
297 .proc_handler = &proc_dointvec,
302 static struct ctl_table abi_root_table[] = {
313 abi_register_sysctl(void)
315 register_sysctl_table(abi_root_table, 1);
319 __initcall(abi_register_sysctl);
322 EXPORT_SYMBOL(abi_defhandler_coff);
323 EXPORT_SYMBOL(abi_defhandler_elf);
324 EXPORT_SYMBOL(abi_defhandler_lcall7);
325 EXPORT_SYMBOL(abi_defhandler_libcso);
326 EXPORT_SYMBOL(abi_traceflg);
327 EXPORT_SYMBOL(abi_fake_utsname);