Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
0004  * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
0005  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
0006  * Copyright 2003 PathScale, Inc.
0007  */
0008 
0009 #include <linux/stddef.h>
0010 #include <linux/err.h>
0011 #include <linux/hardirq.h>
0012 #include <linux/mm.h>
0013 #include <linux/module.h>
0014 #include <linux/personality.h>
0015 #include <linux/proc_fs.h>
0016 #include <linux/ptrace.h>
0017 #include <linux/random.h>
0018 #include <linux/slab.h>
0019 #include <linux/sched.h>
0020 #include <linux/sched/debug.h>
0021 #include <linux/sched/task.h>
0022 #include <linux/sched/task_stack.h>
0023 #include <linux/seq_file.h>
0024 #include <linux/tick.h>
0025 #include <linux/threads.h>
0026 #include <linux/resume_user_mode.h>
0027 #include <asm/current.h>
0028 #include <asm/mmu_context.h>
0029 #include <linux/uaccess.h>
0030 #include <as-layout.h>
0031 #include <kern_util.h>
0032 #include <os.h>
0033 #include <skas.h>
0034 #include <registers.h>
0035 #include <linux/time-internal.h>
0036 
0037 /*
0038  * This is a per-cpu array.  A processor only modifies its entry and it only
0039  * cares about its entry, so it's OK if another processor is modifying its
0040  * entry.
0041  */
0042 struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { -1, NULL } };
0043 
0044 static inline int external_pid(void)
0045 {
0046     /* FIXME: Need to look up userspace_pid by cpu */
0047     return userspace_pid[0];
0048 }
0049 
0050 int pid_to_processor_id(int pid)
0051 {
0052     int i;
0053 
0054     for (i = 0; i < ncpus; i++) {
0055         if (cpu_tasks[i].pid == pid)
0056             return i;
0057     }
0058     return -1;
0059 }
0060 
0061 void free_stack(unsigned long stack, int order)
0062 {
0063     free_pages(stack, order);
0064 }
0065 
0066 unsigned long alloc_stack(int order, int atomic)
0067 {
0068     unsigned long page;
0069     gfp_t flags = GFP_KERNEL;
0070 
0071     if (atomic)
0072         flags = GFP_ATOMIC;
0073     page = __get_free_pages(flags, order);
0074 
0075     return page;
0076 }
0077 
0078 static inline void set_current(struct task_struct *task)
0079 {
0080     cpu_tasks[task_thread_info(task)->cpu] = ((struct cpu_task)
0081         { external_pid(), task });
0082 }
0083 
0084 extern void arch_switch_to(struct task_struct *to);
0085 
0086 void *__switch_to(struct task_struct *from, struct task_struct *to)
0087 {
0088     to->thread.prev_sched = from;
0089     set_current(to);
0090 
0091     switch_threads(&from->thread.switch_buf, &to->thread.switch_buf);
0092     arch_switch_to(current);
0093 
0094     return current->thread.prev_sched;
0095 }
0096 
0097 void interrupt_end(void)
0098 {
0099     struct pt_regs *regs = &current->thread.regs;
0100 
0101     if (need_resched())
0102         schedule();
0103     if (test_thread_flag(TIF_SIGPENDING) ||
0104         test_thread_flag(TIF_NOTIFY_SIGNAL))
0105         do_signal(regs);
0106     if (test_thread_flag(TIF_NOTIFY_RESUME))
0107         resume_user_mode_work(regs);
0108 }
0109 
0110 int get_current_pid(void)
0111 {
0112     return task_pid_nr(current);
0113 }
0114 
0115 /*
0116  * This is called magically, by its address being stuffed in a jmp_buf
0117  * and being longjmp-d to.
0118  */
0119 void new_thread_handler(void)
0120 {
0121     int (*fn)(void *), n;
0122     void *arg;
0123 
0124     if (current->thread.prev_sched != NULL)
0125         schedule_tail(current->thread.prev_sched);
0126     current->thread.prev_sched = NULL;
0127 
0128     fn = current->thread.request.u.thread.proc;
0129     arg = current->thread.request.u.thread.arg;
0130 
0131     /*
0132      * callback returns only if the kernel thread execs a process
0133      */
0134     n = fn(arg);
0135     userspace(&current->thread.regs.regs, current_thread_info()->aux_fp_regs);
0136 }
0137 
0138 /* Called magically, see new_thread_handler above */
0139 void fork_handler(void)
0140 {
0141     force_flush_all();
0142 
0143     schedule_tail(current->thread.prev_sched);
0144 
0145     /*
0146      * XXX: if interrupt_end() calls schedule, this call to
0147      * arch_switch_to isn't needed. We could want to apply this to
0148      * improve performance. -bb
0149      */
0150     arch_switch_to(current);
0151 
0152     current->thread.prev_sched = NULL;
0153 
0154     userspace(&current->thread.regs.regs, current_thread_info()->aux_fp_regs);
0155 }
0156 
0157 int copy_thread(struct task_struct * p, const struct kernel_clone_args *args)
0158 {
0159     unsigned long clone_flags = args->flags;
0160     unsigned long sp = args->stack;
0161     unsigned long tls = args->tls;
0162     void (*handler)(void);
0163     int ret = 0;
0164 
0165     p->thread = (struct thread_struct) INIT_THREAD;
0166 
0167     if (!args->fn) {
0168         memcpy(&p->thread.regs.regs, current_pt_regs(),
0169                sizeof(p->thread.regs.regs));
0170         PT_REGS_SET_SYSCALL_RETURN(&p->thread.regs, 0);
0171         if (sp != 0)
0172             REGS_SP(p->thread.regs.regs.gp) = sp;
0173 
0174         handler = fork_handler;
0175 
0176         arch_copy_thread(&current->thread.arch, &p->thread.arch);
0177     } else {
0178         get_safe_registers(p->thread.regs.regs.gp, p->thread.regs.regs.fp);
0179         p->thread.request.u.thread.proc = args->fn;
0180         p->thread.request.u.thread.arg = args->fn_arg;
0181         handler = new_thread_handler;
0182     }
0183 
0184     new_thread(task_stack_page(p), &p->thread.switch_buf, handler);
0185 
0186     if (!args->fn) {
0187         clear_flushed_tls(p);
0188 
0189         /*
0190          * Set a new TLS for the child thread?
0191          */
0192         if (clone_flags & CLONE_SETTLS)
0193             ret = arch_set_tls(p, tls);
0194     }
0195 
0196     return ret;
0197 }
0198 
0199 void initial_thread_cb(void (*proc)(void *), void *arg)
0200 {
0201     int save_kmalloc_ok = kmalloc_ok;
0202 
0203     kmalloc_ok = 0;
0204     initial_thread_cb_skas(proc, arg);
0205     kmalloc_ok = save_kmalloc_ok;
0206 }
0207 
0208 void um_idle_sleep(void)
0209 {
0210     if (time_travel_mode != TT_MODE_OFF)
0211         time_travel_sleep();
0212     else
0213         os_idle_sleep();
0214 }
0215 
0216 void arch_cpu_idle(void)
0217 {
0218     cpu_tasks[current_thread_info()->cpu].pid = os_getpid();
0219     um_idle_sleep();
0220     raw_local_irq_enable();
0221 }
0222 
0223 int __cant_sleep(void) {
0224     return in_atomic() || irqs_disabled() || in_interrupt();
0225     /* Is in_interrupt() really needed? */
0226 }
0227 
0228 int user_context(unsigned long sp)
0229 {
0230     unsigned long stack;
0231 
0232     stack = sp & (PAGE_MASK << CONFIG_KERNEL_STACK_ORDER);
0233     return stack != (unsigned long) current_thread_info();
0234 }
0235 
0236 extern exitcall_t __uml_exitcall_begin, __uml_exitcall_end;
0237 
0238 void do_uml_exitcalls(void)
0239 {
0240     exitcall_t *call;
0241 
0242     call = &__uml_exitcall_end;
0243     while (--call >= &__uml_exitcall_begin)
0244         (*call)();
0245 }
0246 
0247 char *uml_strdup(const char *string)
0248 {
0249     return kstrdup(string, GFP_KERNEL);
0250 }
0251 EXPORT_SYMBOL(uml_strdup);
0252 
0253 int copy_to_user_proc(void __user *to, void *from, int size)
0254 {
0255     return copy_to_user(to, from, size);
0256 }
0257 
0258 int copy_from_user_proc(void *to, void __user *from, int size)
0259 {
0260     return copy_from_user(to, from, size);
0261 }
0262 
0263 int clear_user_proc(void __user *buf, int size)
0264 {
0265     return clear_user(buf, size);
0266 }
0267 
0268 static atomic_t using_sysemu = ATOMIC_INIT(0);
0269 int sysemu_supported;
0270 
0271 void set_using_sysemu(int value)
0272 {
0273     if (value > sysemu_supported)
0274         return;
0275     atomic_set(&using_sysemu, value);
0276 }
0277 
0278 int get_using_sysemu(void)
0279 {
0280     return atomic_read(&using_sysemu);
0281 }
0282 
0283 static int sysemu_proc_show(struct seq_file *m, void *v)
0284 {
0285     seq_printf(m, "%d\n", get_using_sysemu());
0286     return 0;
0287 }
0288 
0289 static int sysemu_proc_open(struct inode *inode, struct file *file)
0290 {
0291     return single_open(file, sysemu_proc_show, NULL);
0292 }
0293 
0294 static ssize_t sysemu_proc_write(struct file *file, const char __user *buf,
0295                  size_t count, loff_t *pos)
0296 {
0297     char tmp[2];
0298 
0299     if (copy_from_user(tmp, buf, 1))
0300         return -EFAULT;
0301 
0302     if (tmp[0] >= '0' && tmp[0] <= '2')
0303         set_using_sysemu(tmp[0] - '0');
0304     /* We use the first char, but pretend to write everything */
0305     return count;
0306 }
0307 
0308 static const struct proc_ops sysemu_proc_ops = {
0309     .proc_open  = sysemu_proc_open,
0310     .proc_read  = seq_read,
0311     .proc_lseek = seq_lseek,
0312     .proc_release   = single_release,
0313     .proc_write = sysemu_proc_write,
0314 };
0315 
0316 int __init make_proc_sysemu(void)
0317 {
0318     struct proc_dir_entry *ent;
0319     if (!sysemu_supported)
0320         return 0;
0321 
0322     ent = proc_create("sysemu", 0600, NULL, &sysemu_proc_ops);
0323 
0324     if (ent == NULL)
0325     {
0326         printk(KERN_WARNING "Failed to register /proc/sysemu\n");
0327         return 0;
0328     }
0329 
0330     return 0;
0331 }
0332 
0333 late_initcall(make_proc_sysemu);
0334 
0335 int singlestepping(void * t)
0336 {
0337     struct task_struct *task = t ? t : current;
0338 
0339     if (!test_thread_flag(TIF_SINGLESTEP))
0340         return 0;
0341 
0342     if (task->thread.singlestep_syscall)
0343         return 1;
0344 
0345     return 2;
0346 }
0347 
0348 /*
0349  * Only x86 and x86_64 have an arch_align_stack().
0350  * All other arches have "#define arch_align_stack(x) (x)"
0351  * in their asm/exec.h
0352  * As this is included in UML from asm-um/system-generic.h,
0353  * we can use it to behave as the subarch does.
0354  */
0355 #ifndef arch_align_stack
0356 unsigned long arch_align_stack(unsigned long sp)
0357 {
0358     if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
0359         sp -= get_random_int() % 8192;
0360     return sp & ~0xf;
0361 }
0362 #endif
0363 
0364 unsigned long __get_wchan(struct task_struct *p)
0365 {
0366     unsigned long stack_page, sp, ip;
0367     bool seen_sched = 0;
0368 
0369     stack_page = (unsigned long) task_stack_page(p);
0370     /* Bail if the process has no kernel stack for some reason */
0371     if (stack_page == 0)
0372         return 0;
0373 
0374     sp = p->thread.switch_buf->JB_SP;
0375     /*
0376      * Bail if the stack pointer is below the bottom of the kernel
0377      * stack for some reason
0378      */
0379     if (sp < stack_page)
0380         return 0;
0381 
0382     while (sp < stack_page + THREAD_SIZE) {
0383         ip = *((unsigned long *) sp);
0384         if (in_sched_functions(ip))
0385             /* Ignore everything until we're above the scheduler */
0386             seen_sched = 1;
0387         else if (kernel_text_address(ip) && seen_sched)
0388             return ip;
0389 
0390         sp += sizeof(unsigned long);
0391     }
0392 
0393     return 0;
0394 }
0395 
0396 int elf_core_copy_fpregs(struct task_struct *t, elf_fpregset_t *fpu)
0397 {
0398     int cpu = current_thread_info()->cpu;
0399 
0400     return save_i387_registers(userspace_pid[cpu], (unsigned long *) fpu);
0401 }
0402