blob: c6c45ea4021ff0547353bf5ca889c80abea87205 [file] [log] [blame]
Jeff Dike995473a2006-09-27 01:50:40 -07001/*
Anton Ivanov2eb5f312015-11-02 16:16:37 +00002 * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
3 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
Jeff Dikeba180fd2007-10-16 01:27:00 -07004 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright 2003 PathScale, Inc.
6 * Licensed under the GPL
7 */
8
Jeff Dikec5d4bb12008-02-04 22:31:14 -08009#include <linux/stddef.h>
10#include <linux/err.h>
11#include <linux/hardirq.h>
Jeff Dikec5d4bb12008-02-04 22:31:14 -080012#include <linux/mm.h>
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -080013#include <linux/module.h>
Jeff Dikec5d4bb12008-02-04 22:31:14 -080014#include <linux/personality.h>
15#include <linux/proc_fs.h>
16#include <linux/ptrace.h>
17#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Jeff Dikec5d4bb12008-02-04 22:31:14 -080019#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010020#include <linux/sched/debug.h>
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -080021#include <linux/seq_file.h>
Jeff Dikec5d4bb12008-02-04 22:31:14 -080022#include <linux/tick.h>
23#include <linux/threads.h>
Al Virod50349b2012-04-24 02:37:07 -040024#include <linux/tracehook.h>
Jeff Dikec5d4bb12008-02-04 22:31:14 -080025#include <asm/current.h>
26#include <asm/pgtable.h>
Al Viro445c5782011-08-18 20:07:59 +010027#include <asm/mmu_context.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080028#include <linux/uaccess.h>
Al Viro37185b32012-10-08 03:27:32 +010029#include <as-layout.h>
30#include <kern_util.h>
31#include <os.h>
32#include <skas.h>
Anton Ivanov2eb5f312015-11-02 16:16:37 +000033#include <timer-internal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Jeff Dikeba180fd2007-10-16 01:27:00 -070035/*
36 * This is a per-cpu array. A processor only modifies its entry and it only
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * cares about its entry, so it's OK if another processor is modifying its
38 * entry.
39 */
40struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
41
Karol Swietlicki2dc58022008-02-04 22:31:03 -080042static inline int external_pid(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Jeff Dike77bf4402007-10-16 01:26:58 -070044 /* FIXME: Need to look up userspace_pid by cpu */
Jeff Dikeba180fd2007-10-16 01:27:00 -070045 return userspace_pid[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
48int pid_to_processor_id(int pid)
49{
50 int i;
51
Jeff Dikec5d4bb12008-02-04 22:31:14 -080052 for (i = 0; i < ncpus; i++) {
Jeff Dikeba180fd2007-10-16 01:27:00 -070053 if (cpu_tasks[i].pid == pid)
Jeff Dike6e21aec2007-05-06 14:51:21 -070054 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 }
Jeff Dike6e21aec2007-05-06 14:51:21 -070056 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
59void free_stack(unsigned long stack, int order)
60{
61 free_pages(stack, order);
62}
63
64unsigned long alloc_stack(int order, int atomic)
65{
66 unsigned long page;
Al Viro53f9fc92005-10-21 03:22:24 -040067 gfp_t flags = GFP_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Paolo 'Blaisorblade' Giarrusso46db4a42d2005-09-22 21:44:20 -070069 if (atomic)
70 flags = GFP_ATOMIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 page = __get_free_pages(flags, order);
Jeff Dike5c8aace2007-10-16 01:26:46 -070072
Jeff Dike6e21aec2007-05-06 14:51:21 -070073 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
Jeff Dike6e21aec2007-05-06 14:51:21 -070076static inline void set_current(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Al Viroca9bc0b2006-01-12 01:05:48 -080078 cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task)
Karol Swietlicki2dc58022008-02-04 22:31:03 -080079 { external_pid(), task });
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Karol Swietlicki291248f2008-02-04 22:30:49 -080082extern void arch_switch_to(struct task_struct *to);
Jeff Dike77bf4402007-10-16 01:26:58 -070083
Richard Weinberger76b278e2012-03-29 19:10:42 +020084void *__switch_to(struct task_struct *from, struct task_struct *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Jeff Dike995473a2006-09-27 01:50:40 -070086 to->thread.prev_sched = from;
87 set_current(to);
Jeff Dikef6e34c62005-09-16 19:27:43 -070088
Richard Weinbergera1850e92013-09-23 17:38:03 +020089 switch_threads(&from->thread.switch_buf, &to->thread.switch_buf);
90 arch_switch_to(current);
Jeff Dikef6e34c62005-09-16 19:27:43 -070091
Jeff Dike6e21aec2007-05-06 14:51:21 -070092 return current->thread.prev_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95void interrupt_end(void)
96{
Ingo Molnarccaee5f2015-07-03 12:44:20 -070097 struct pt_regs *regs = &current->thread.regs;
98
Jeff Dikeba180fd2007-10-16 01:27:00 -070099 if (need_resched())
Jeff Dike6e21aec2007-05-06 14:51:21 -0700100 schedule();
Al Virod50349b2012-04-24 02:37:07 -0400101 if (test_thread_flag(TIF_SIGPENDING))
Ingo Molnarccaee5f2015-07-03 12:44:20 -0700102 do_signal(regs);
Al Viroa42c6de2012-05-23 14:44:37 -0400103 if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
Ingo Molnarccaee5f2015-07-03 12:44:20 -0700104 tracehook_notify_resume(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Al Viroc2220b22012-01-30 16:30:48 -0500107int get_current_pid(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Al Viroc2220b22012-01-30 16:30:48 -0500109 return task_pid_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Jeff Dikeba180fd2007-10-16 01:27:00 -0700112/*
113 * This is called magically, by its address being stuffed in a jmp_buf
Jeff Dike77bf4402007-10-16 01:26:58 -0700114 * and being longjmp-d to.
115 */
116void new_thread_handler(void)
117{
118 int (*fn)(void *), n;
119 void *arg;
120
Jeff Dikeba180fd2007-10-16 01:27:00 -0700121 if (current->thread.prev_sched != NULL)
Jeff Dike77bf4402007-10-16 01:26:58 -0700122 schedule_tail(current->thread.prev_sched);
123 current->thread.prev_sched = NULL;
124
125 fn = current->thread.request.u.thread.proc;
126 arg = current->thread.request.u.thread.arg;
127
Jeff Dikeba180fd2007-10-16 01:27:00 -0700128 /*
Al Viro22e2430d2012-10-10 21:35:42 -0400129 * callback returns only if the kernel thread execs a process
Jeff Dike77bf4402007-10-16 01:26:58 -0700130 */
Al Viro22e2430d2012-10-10 21:35:42 -0400131 n = fn(arg);
132 userspace(&current->thread.regs.regs);
Jeff Dike77bf4402007-10-16 01:26:58 -0700133}
134
135/* Called magically, see new_thread_handler above */
136void fork_handler(void)
137{
138 force_flush_all();
Jeff Dike77bf4402007-10-16 01:26:58 -0700139
140 schedule_tail(current->thread.prev_sched);
141
Jeff Dikeba180fd2007-10-16 01:27:00 -0700142 /*
143 * XXX: if interrupt_end() calls schedule, this call to
Jeff Dike77bf4402007-10-16 01:26:58 -0700144 * arch_switch_to isn't needed. We could want to apply this to
Jeff Dikeba180fd2007-10-16 01:27:00 -0700145 * improve performance. -bb
146 */
Karol Swietlicki291248f2008-02-04 22:30:49 -0800147 arch_switch_to(current);
Jeff Dike77bf4402007-10-16 01:26:58 -0700148
149 current->thread.prev_sched = NULL;
150
Jeff Dike77bf4402007-10-16 01:26:58 -0700151 userspace(&current->thread.regs.regs);
152}
153
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700154int copy_thread(unsigned long clone_flags, unsigned long sp,
Al Viroafa86fc2012-10-22 22:51:14 -0400155 unsigned long arg, struct task_struct * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Jeff Dike77bf4402007-10-16 01:26:58 -0700157 void (*handler)(void);
Al Virod2ce4e92012-09-20 09:28:25 -0400158 int kthread = current->flags & PF_KTHREAD;
Jeff Dike77bf4402007-10-16 01:26:58 -0700159 int ret = 0;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 p->thread = (struct thread_struct) INIT_THREAD;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800162
Al Virod2ce4e92012-09-20 09:28:25 -0400163 if (!kthread) {
Al Viro2b067fc2012-10-29 21:36:45 -0400164 memcpy(&p->thread.regs.regs, current_pt_regs(),
Jeff Dike77bf4402007-10-16 01:26:58 -0700165 sizeof(p->thread.regs.regs));
Al Viroa3170d22012-05-22 21:16:35 -0400166 PT_REGS_SET_SYSCALL_RETURN(&p->thread.regs, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700167 if (sp != 0)
Jeff Dike18baddd2007-10-16 01:27:07 -0700168 REGS_SP(p->thread.regs.regs.gp) = sp;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800169
Jeff Dike77bf4402007-10-16 01:26:58 -0700170 handler = fork_handler;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800171
Jeff Dike77bf4402007-10-16 01:26:58 -0700172 arch_copy_thread(&current->thread.arch, &p->thread.arch);
Al Virod2ce4e92012-09-20 09:28:25 -0400173 } else {
Ingo van Lilfbfe9c82011-09-14 16:21:23 -0700174 get_safe_registers(p->thread.regs.regs.gp, p->thread.regs.regs.fp);
Al Viro1f02ab42012-09-21 20:32:29 -0400175 p->thread.request.u.thread.proc = (int (*)(void *))sp;
176 p->thread.request.u.thread.arg = (void *)arg;
Jeff Dike77bf4402007-10-16 01:26:58 -0700177 handler = new_thread_handler;
178 }
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800179
Jeff Dike77bf4402007-10-16 01:26:58 -0700180 new_thread(task_stack_page(p), &p->thread.switch_buf, handler);
181
Al Virod2ce4e92012-09-20 09:28:25 -0400182 if (!kthread) {
Jeff Dike77bf4402007-10-16 01:26:58 -0700183 clear_flushed_tls(p);
184
185 /*
186 * Set a new TLS for the child thread?
187 */
188 if (clone_flags & CLONE_SETTLS)
189 ret = arch_copy_tls(p);
190 }
191
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800192 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195void initial_thread_cb(void (*proc)(void *), void *arg)
196{
197 int save_kmalloc_ok = kmalloc_ok;
198
199 kmalloc_ok = 0;
Jeff Dike6aa802c2007-10-16 01:26:56 -0700200 initial_thread_cb_skas(proc, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 kmalloc_ok = save_kmalloc_ok;
202}
Jeff Dike995473a2006-09-27 01:50:40 -0700203
Richard Weinberger8198c162013-04-16 23:53:29 +0200204void arch_cpu_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Jeff Dikea5a678c2008-02-04 22:30:54 -0800206 cpu_tasks[current_thread_info()->cpu].pid = os_getpid();
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000207 os_idle_sleep(UM_NSEC_PER_SEC);
Richard Weinberger8198c162013-04-16 23:53:29 +0200208 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Paolo 'Blaisorblade' Giarrussob6316292006-01-18 17:42:58 -0800211int __cant_sleep(void) {
212 return in_atomic() || irqs_disabled() || in_interrupt();
213 /* Is in_interrupt() really needed? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216int user_context(unsigned long sp)
217{
218 unsigned long stack;
219
220 stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
Jeff Dikea5a678c2008-02-04 22:30:54 -0800221 return stack != (unsigned long) current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
225
226void do_uml_exitcalls(void)
227{
228 exitcall_t *call;
229
230 call = &__uml_exitcall_end;
231 while (--call >= &__uml_exitcall_begin)
232 (*call)();
233}
234
WANG Congc0a92902008-02-04 22:30:41 -0800235char *uml_strdup(const char *string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Robert Lovedfe52242005-06-23 00:09:04 -0700237 return kstrdup(string, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
Al Viro73395a02011-08-18 20:14:10 +0100239EXPORT_SYMBOL(uml_strdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241int copy_to_user_proc(void __user *to, void *from, int size)
242{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700243 return copy_to_user(to, from, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246int copy_from_user_proc(void *to, void __user *from, int size)
247{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700248 return copy_from_user(to, from, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251int clear_user_proc(void __user *buf, int size)
252{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700253 return clear_user(buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256int strlen_user_proc(char __user *str)
257{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700258 return strlen_user(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261int cpu(void)
262{
Jeff Dikea5a678c2008-02-04 22:30:54 -0800263 return current_thread_info()->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266static atomic_t using_sysemu = ATOMIC_INIT(0);
267int sysemu_supported;
268
269void set_using_sysemu(int value)
270{
271 if (value > sysemu_supported)
272 return;
273 atomic_set(&using_sysemu, value);
274}
275
276int get_using_sysemu(void)
277{
278 return atomic_read(&using_sysemu);
279}
280
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -0800281static int sysemu_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -0800283 seq_printf(m, "%d\n", get_using_sysemu());
284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -0800287static int sysemu_proc_open(struct inode *inode, struct file *file)
288{
289 return single_open(file, sysemu_proc_show, NULL);
290}
291
292static ssize_t sysemu_proc_write(struct file *file, const char __user *buf,
293 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 char tmp[2];
296
297 if (copy_from_user(tmp, buf, 1))
298 return -EFAULT;
299
300 if (tmp[0] >= '0' && tmp[0] <= '2')
301 set_using_sysemu(tmp[0] - '0');
Jeff Dikeba180fd2007-10-16 01:27:00 -0700302 /* We use the first char, but pretend to write everything */
303 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -0800306static const struct file_operations sysemu_proc_fops = {
307 .owner = THIS_MODULE,
308 .open = sysemu_proc_open,
309 .read = seq_read,
310 .llseek = seq_lseek,
311 .release = single_release,
312 .write = sysemu_proc_write,
313};
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315int __init make_proc_sysemu(void)
316{
317 struct proc_dir_entry *ent;
318 if (!sysemu_supported)
319 return 0;
320
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -0800321 ent = proc_create("sysemu", 0600, NULL, &sysemu_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 if (ent == NULL)
324 {
Christophe Lucas30f417c2005-07-28 21:16:12 -0700325 printk(KERN_WARNING "Failed to register /proc/sysemu\n");
Jeff Dike6e21aec2007-05-06 14:51:21 -0700326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 0;
330}
331
332late_initcall(make_proc_sysemu);
333
334int singlestepping(void * t)
335{
336 struct task_struct *task = t ? t : current;
337
Jeff Dikec5d4bb12008-02-04 22:31:14 -0800338 if (!(task->ptrace & PT_DTRACE))
Jeff Dikeba180fd2007-10-16 01:27:00 -0700339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (task->thread.singlestep_syscall)
Jeff Dikeba180fd2007-10-16 01:27:00 -0700342 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 return 2;
345}
346
Bodo Stroesserb8bd0222005-05-06 21:30:53 -0700347/*
348 * Only x86 and x86_64 have an arch_align_stack().
349 * All other arches have "#define arch_align_stack(x) (x)"
David Howellscf7bc582014-04-07 15:39:22 -0700350 * in their asm/exec.h
Bodo Stroesserb8bd0222005-05-06 21:30:53 -0700351 * As this is included in UML from asm-um/system-generic.h,
352 * we can use it to behave as the subarch does.
353 */
354#ifndef arch_align_stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355unsigned long arch_align_stack(unsigned long sp)
356{
Jeff Dike8f80e942006-09-25 23:33:01 -0700357 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 sp -= get_random_int() % 8192;
359 return sp & ~0xf;
360}
Bodo Stroesserb8bd0222005-05-06 21:30:53 -0700361#endif
Jeff Dikec1127462008-02-04 22:30:36 -0800362
363unsigned long get_wchan(struct task_struct *p)
364{
365 unsigned long stack_page, sp, ip;
366 bool seen_sched = 0;
367
368 if ((p == NULL) || (p == current) || (p->state == TASK_RUNNING))
369 return 0;
370
371 stack_page = (unsigned long) task_stack_page(p);
372 /* Bail if the process has no kernel stack for some reason */
373 if (stack_page == 0)
374 return 0;
375
376 sp = p->thread.switch_buf->JB_SP;
377 /*
378 * Bail if the stack pointer is below the bottom of the kernel
379 * stack for some reason
380 */
381 if (sp < stack_page)
382 return 0;
383
384 while (sp < stack_page + THREAD_SIZE) {
385 ip = *((unsigned long *) sp);
386 if (in_sched_functions(ip))
387 /* Ignore everything until we're above the scheduler */
388 seen_sched = 1;
389 else if (kernel_text_address(ip) && seen_sched)
390 return ip;
391
392 sp += sizeof(unsigned long);
393 }
394
395 return 0;
396}
Jeff Dike8192ab42008-02-04 22:30:53 -0800397
398int elf_core_copy_fpregs(struct task_struct *t, elf_fpregset_t *fpu)
399{
400 int cpu = current_thread_info()->cpu;
401
Eli Coopera78ff112016-03-20 00:58:41 +0800402 return save_i387_registers(userspace_pid[cpu], (unsigned long *) fpu);
Jeff Dike8192ab42008-02-04 22:30:53 -0800403}
404