blob: e76dc7c112519dddba9b3df248e1a033a4d5f2df [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>
Ingo Molnar29930022017-02-08 18:51:36 +010021#include <linux/sched/task.h>
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -080022#include <linux/seq_file.h>
Jeff Dikec5d4bb12008-02-04 22:31:14 -080023#include <linux/tick.h>
24#include <linux/threads.h>
Al Virod50349b2012-04-24 02:37:07 -040025#include <linux/tracehook.h>
Jeff Dikec5d4bb12008-02-04 22:31:14 -080026#include <asm/current.h>
27#include <asm/pgtable.h>
Al Viro445c5782011-08-18 20:07:59 +010028#include <asm/mmu_context.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Al Viro37185b32012-10-08 03:27:32 +010030#include <as-layout.h>
31#include <kern_util.h>
32#include <os.h>
33#include <skas.h>
Anton Ivanov2eb5f312015-11-02 16:16:37 +000034#include <timer-internal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Jeff Dikeba180fd2007-10-16 01:27:00 -070036/*
37 * This is a per-cpu array. A processor only modifies its entry and it only
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * cares about its entry, so it's OK if another processor is modifying its
39 * entry.
40 */
41struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
42
Karol Swietlicki2dc58022008-02-04 22:31:03 -080043static inline int external_pid(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Jeff Dike77bf4402007-10-16 01:26:58 -070045 /* FIXME: Need to look up userspace_pid by cpu */
Jeff Dikeba180fd2007-10-16 01:27:00 -070046 return userspace_pid[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
49int pid_to_processor_id(int pid)
50{
51 int i;
52
Jeff Dikec5d4bb12008-02-04 22:31:14 -080053 for (i = 0; i < ncpus; i++) {
Jeff Dikeba180fd2007-10-16 01:27:00 -070054 if (cpu_tasks[i].pid == pid)
Jeff Dike6e21aec2007-05-06 14:51:21 -070055 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 }
Jeff Dike6e21aec2007-05-06 14:51:21 -070057 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
60void free_stack(unsigned long stack, int order)
61{
62 free_pages(stack, order);
63}
64
65unsigned long alloc_stack(int order, int atomic)
66{
67 unsigned long page;
Al Viro53f9fc92005-10-21 03:22:24 -040068 gfp_t flags = GFP_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Paolo 'Blaisorblade' Giarrusso46db4a42d2005-09-22 21:44:20 -070070 if (atomic)
71 flags = GFP_ATOMIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 page = __get_free_pages(flags, order);
Jeff Dike5c8aace2007-10-16 01:26:46 -070073
Jeff Dike6e21aec2007-05-06 14:51:21 -070074 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Jeff Dike6e21aec2007-05-06 14:51:21 -070077static inline void set_current(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Al Viroca9bc0b2006-01-12 01:05:48 -080079 cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task)
Karol Swietlicki2dc58022008-02-04 22:31:03 -080080 { external_pid(), task });
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Karol Swietlicki291248f2008-02-04 22:30:49 -080083extern void arch_switch_to(struct task_struct *to);
Jeff Dike77bf4402007-10-16 01:26:58 -070084
Richard Weinberger76b278e2012-03-29 19:10:42 +020085void *__switch_to(struct task_struct *from, struct task_struct *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Jeff Dike995473a2006-09-27 01:50:40 -070087 to->thread.prev_sched = from;
88 set_current(to);
Jeff Dikef6e34c62005-09-16 19:27:43 -070089
Richard Weinbergera1850e92013-09-23 17:38:03 +020090 switch_threads(&from->thread.switch_buf, &to->thread.switch_buf);
91 arch_switch_to(current);
Jeff Dikef6e34c62005-09-16 19:27:43 -070092
Jeff Dike6e21aec2007-05-06 14:51:21 -070093 return current->thread.prev_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96void interrupt_end(void)
97{
Ingo Molnarccaee5f2015-07-03 12:44:20 -070098 struct pt_regs *regs = &current->thread.regs;
99
Jeff Dikeba180fd2007-10-16 01:27:00 -0700100 if (need_resched())
Jeff Dike6e21aec2007-05-06 14:51:21 -0700101 schedule();
Al Virod50349b2012-04-24 02:37:07 -0400102 if (test_thread_flag(TIF_SIGPENDING))
Ingo Molnarccaee5f2015-07-03 12:44:20 -0700103 do_signal(regs);
Al Viroa42c6de2012-05-23 14:44:37 -0400104 if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
Ingo Molnarccaee5f2015-07-03 12:44:20 -0700105 tracehook_notify_resume(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Al Viroc2220b22012-01-30 16:30:48 -0500108int get_current_pid(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Al Viroc2220b22012-01-30 16:30:48 -0500110 return task_pid_nr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Jeff Dikeba180fd2007-10-16 01:27:00 -0700113/*
114 * This is called magically, by its address being stuffed in a jmp_buf
Jeff Dike77bf4402007-10-16 01:26:58 -0700115 * and being longjmp-d to.
116 */
117void new_thread_handler(void)
118{
119 int (*fn)(void *), n;
120 void *arg;
121
Jeff Dikeba180fd2007-10-16 01:27:00 -0700122 if (current->thread.prev_sched != NULL)
Jeff Dike77bf4402007-10-16 01:26:58 -0700123 schedule_tail(current->thread.prev_sched);
124 current->thread.prev_sched = NULL;
125
126 fn = current->thread.request.u.thread.proc;
127 arg = current->thread.request.u.thread.arg;
128
Jeff Dikeba180fd2007-10-16 01:27:00 -0700129 /*
Al Viro22e2430d2012-10-10 21:35:42 -0400130 * callback returns only if the kernel thread execs a process
Jeff Dike77bf4402007-10-16 01:26:58 -0700131 */
Al Viro22e2430d2012-10-10 21:35:42 -0400132 n = fn(arg);
133 userspace(&current->thread.regs.regs);
Jeff Dike77bf4402007-10-16 01:26:58 -0700134}
135
136/* Called magically, see new_thread_handler above */
137void fork_handler(void)
138{
139 force_flush_all();
Jeff Dike77bf4402007-10-16 01:26:58 -0700140
141 schedule_tail(current->thread.prev_sched);
142
Jeff Dikeba180fd2007-10-16 01:27:00 -0700143 /*
144 * XXX: if interrupt_end() calls schedule, this call to
Jeff Dike77bf4402007-10-16 01:26:58 -0700145 * arch_switch_to isn't needed. We could want to apply this to
Jeff Dikeba180fd2007-10-16 01:27:00 -0700146 * improve performance. -bb
147 */
Karol Swietlicki291248f2008-02-04 22:30:49 -0800148 arch_switch_to(current);
Jeff Dike77bf4402007-10-16 01:26:58 -0700149
150 current->thread.prev_sched = NULL;
151
Jeff Dike77bf4402007-10-16 01:26:58 -0700152 userspace(&current->thread.regs.regs);
153}
154
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700155int copy_thread(unsigned long clone_flags, unsigned long sp,
Al Viroafa86fc2012-10-22 22:51:14 -0400156 unsigned long arg, struct task_struct * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Jeff Dike77bf4402007-10-16 01:26:58 -0700158 void (*handler)(void);
Al Virod2ce4e92012-09-20 09:28:25 -0400159 int kthread = current->flags & PF_KTHREAD;
Jeff Dike77bf4402007-10-16 01:26:58 -0700160 int ret = 0;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 p->thread = (struct thread_struct) INIT_THREAD;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800163
Al Virod2ce4e92012-09-20 09:28:25 -0400164 if (!kthread) {
Al Viro2b067fc2012-10-29 21:36:45 -0400165 memcpy(&p->thread.regs.regs, current_pt_regs(),
Jeff Dike77bf4402007-10-16 01:26:58 -0700166 sizeof(p->thread.regs.regs));
Al Viroa3170d22012-05-22 21:16:35 -0400167 PT_REGS_SET_SYSCALL_RETURN(&p->thread.regs, 0);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700168 if (sp != 0)
Jeff Dike18baddd2007-10-16 01:27:07 -0700169 REGS_SP(p->thread.regs.regs.gp) = sp;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800170
Jeff Dike77bf4402007-10-16 01:26:58 -0700171 handler = fork_handler;
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800172
Jeff Dike77bf4402007-10-16 01:26:58 -0700173 arch_copy_thread(&current->thread.arch, &p->thread.arch);
Al Virod2ce4e92012-09-20 09:28:25 -0400174 } else {
Ingo van Lilfbfe9c82011-09-14 16:21:23 -0700175 get_safe_registers(p->thread.regs.regs.gp, p->thread.regs.regs.fp);
Al Viro1f02ab42012-09-21 20:32:29 -0400176 p->thread.request.u.thread.proc = (int (*)(void *))sp;
177 p->thread.request.u.thread.arg = (void *)arg;
Jeff Dike77bf4402007-10-16 01:26:58 -0700178 handler = new_thread_handler;
179 }
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800180
Jeff Dike77bf4402007-10-16 01:26:58 -0700181 new_thread(task_stack_page(p), &p->thread.switch_buf, handler);
182
Al Virod2ce4e92012-09-20 09:28:25 -0400183 if (!kthread) {
Jeff Dike77bf4402007-10-16 01:26:58 -0700184 clear_flushed_tls(p);
185
186 /*
187 * Set a new TLS for the child thread?
188 */
189 if (clone_flags & CLONE_SETTLS)
190 ret = arch_copy_tls(p);
191 }
192
Paolo 'Blaisorblade' Giarrussoaa6758d2006-03-31 02:30:22 -0800193 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196void initial_thread_cb(void (*proc)(void *), void *arg)
197{
198 int save_kmalloc_ok = kmalloc_ok;
199
200 kmalloc_ok = 0;
Jeff Dike6aa802c2007-10-16 01:26:56 -0700201 initial_thread_cb_skas(proc, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 kmalloc_ok = save_kmalloc_ok;
203}
Jeff Dike995473a2006-09-27 01:50:40 -0700204
Richard Weinberger8198c162013-04-16 23:53:29 +0200205void arch_cpu_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Jeff Dikea5a678c2008-02-04 22:30:54 -0800207 cpu_tasks[current_thread_info()->cpu].pid = os_getpid();
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000208 os_idle_sleep(UM_NSEC_PER_SEC);
Richard Weinberger8198c162013-04-16 23:53:29 +0200209 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Paolo 'Blaisorblade' Giarrussob6316292006-01-18 17:42:58 -0800212int __cant_sleep(void) {
213 return in_atomic() || irqs_disabled() || in_interrupt();
214 /* Is in_interrupt() really needed? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217int user_context(unsigned long sp)
218{
219 unsigned long stack;
220
221 stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
Jeff Dikea5a678c2008-02-04 22:30:54 -0800222 return stack != (unsigned long) current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
226
227void do_uml_exitcalls(void)
228{
229 exitcall_t *call;
230
231 call = &__uml_exitcall_end;
232 while (--call >= &__uml_exitcall_begin)
233 (*call)();
234}
235
WANG Congc0a92902008-02-04 22:30:41 -0800236char *uml_strdup(const char *string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Robert Lovedfe52242005-06-23 00:09:04 -0700238 return kstrdup(string, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
Al Viro73395a02011-08-18 20:14:10 +0100240EXPORT_SYMBOL(uml_strdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242int copy_to_user_proc(void __user *to, void *from, int size)
243{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700244 return copy_to_user(to, from, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
247int copy_from_user_proc(void *to, void __user *from, int size)
248{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700249 return copy_from_user(to, from, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252int clear_user_proc(void __user *buf, int size)
253{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700254 return clear_user(buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257int strlen_user_proc(char __user *str)
258{
Jeff Dike6e21aec2007-05-06 14:51:21 -0700259 return strlen_user(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262int cpu(void)
263{
Jeff Dikea5a678c2008-02-04 22:30:54 -0800264 return current_thread_info()->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267static atomic_t using_sysemu = ATOMIC_INIT(0);
268int sysemu_supported;
269
270void set_using_sysemu(int value)
271{
272 if (value > sysemu_supported)
273 return;
274 atomic_set(&using_sysemu, value);
275}
276
277int get_using_sysemu(void)
278{
279 return atomic_read(&using_sysemu);
280}
281
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -0800282static int sysemu_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -0800284 seq_printf(m, "%d\n", get_using_sysemu());
285 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -0800288static int sysemu_proc_open(struct inode *inode, struct file *file)
289{
290 return single_open(file, sysemu_proc_show, NULL);
291}
292
293static ssize_t sysemu_proc_write(struct file *file, const char __user *buf,
294 size_t count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 char tmp[2];
297
298 if (copy_from_user(tmp, buf, 1))
299 return -EFAULT;
300
301 if (tmp[0] >= '0' && tmp[0] <= '2')
302 set_using_sysemu(tmp[0] - '0');
Jeff Dikeba180fd2007-10-16 01:27:00 -0700303 /* We use the first char, but pretend to write everything */
304 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -0800307static const struct file_operations sysemu_proc_fops = {
308 .owner = THIS_MODULE,
309 .open = sysemu_proc_open,
310 .read = seq_read,
311 .llseek = seq_lseek,
312 .release = single_release,
313 .write = sysemu_proc_write,
314};
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316int __init make_proc_sysemu(void)
317{
318 struct proc_dir_entry *ent;
319 if (!sysemu_supported)
320 return 0;
321
Alexey Dobriyan6613c5e2009-12-14 18:00:11 -0800322 ent = proc_create("sysemu", 0600, NULL, &sysemu_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 if (ent == NULL)
325 {
Christophe Lucas30f417c2005-07-28 21:16:12 -0700326 printk(KERN_WARNING "Failed to register /proc/sysemu\n");
Jeff Dike6e21aec2007-05-06 14:51:21 -0700327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return 0;
331}
332
333late_initcall(make_proc_sysemu);
334
335int singlestepping(void * t)
336{
337 struct task_struct *task = t ? t : current;
338
Jeff Dikec5d4bb12008-02-04 22:31:14 -0800339 if (!(task->ptrace & PT_DTRACE))
Jeff Dikeba180fd2007-10-16 01:27:00 -0700340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 if (task->thread.singlestep_syscall)
Jeff Dikeba180fd2007-10-16 01:27:00 -0700343 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 return 2;
346}
347
Bodo Stroesserb8bd0222005-05-06 21:30:53 -0700348/*
349 * Only x86 and x86_64 have an arch_align_stack().
350 * All other arches have "#define arch_align_stack(x) (x)"
David Howellscf7bc582014-04-07 15:39:22 -0700351 * in their asm/exec.h
Bodo Stroesserb8bd0222005-05-06 21:30:53 -0700352 * As this is included in UML from asm-um/system-generic.h,
353 * we can use it to behave as the subarch does.
354 */
355#ifndef arch_align_stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356unsigned long arch_align_stack(unsigned long sp)
357{
Jeff Dike8f80e942006-09-25 23:33:01 -0700358 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 sp -= get_random_int() % 8192;
360 return sp & ~0xf;
361}
Bodo Stroesserb8bd0222005-05-06 21:30:53 -0700362#endif
Jeff Dikec1127462008-02-04 22:30:36 -0800363
364unsigned long get_wchan(struct task_struct *p)
365{
366 unsigned long stack_page, sp, ip;
367 bool seen_sched = 0;
368
369 if ((p == NULL) || (p == current) || (p->state == TASK_RUNNING))
370 return 0;
371
372 stack_page = (unsigned long) task_stack_page(p);
373 /* Bail if the process has no kernel stack for some reason */
374 if (stack_page == 0)
375 return 0;
376
377 sp = p->thread.switch_buf->JB_SP;
378 /*
379 * Bail if the stack pointer is below the bottom of the kernel
380 * stack for some reason
381 */
382 if (sp < stack_page)
383 return 0;
384
385 while (sp < stack_page + THREAD_SIZE) {
386 ip = *((unsigned long *) sp);
387 if (in_sched_functions(ip))
388 /* Ignore everything until we're above the scheduler */
389 seen_sched = 1;
390 else if (kernel_text_address(ip) && seen_sched)
391 return ip;
392
393 sp += sizeof(unsigned long);
394 }
395
396 return 0;
397}
Jeff Dike8192ab42008-02-04 22:30:53 -0800398
399int elf_core_copy_fpregs(struct task_struct *t, elf_fpregset_t *fpu)
400{
401 int cpu = current_thread_info()->cpu;
402
Eli Coopera78ff112016-03-20 00:58:41 +0800403 return save_i387_registers(userspace_pid[cpu], (unsigned long *) fpu);
Jeff Dike8192ab42008-02-04 22:30:53 -0800404}
405