linux/arch/metag/kernel/process.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2005,2006,2007,2008,2009,2010,2011 Imagination Technologies
   3 *
   4 * This file contains the architecture-dependent parts of process handling.
   5 *
   6 */
   7
   8#include <linux/errno.h>
   9#include <linux/export.h>
  10#include <linux/sched.h>
  11#include <linux/kernel.h>
  12#include <linux/mm.h>
  13#include <linux/unistd.h>
  14#include <linux/ptrace.h>
  15#include <linux/user.h>
  16#include <linux/reboot.h>
  17#include <linux/elfcore.h>
  18#include <linux/fs.h>
  19#include <linux/tick.h>
  20#include <linux/slab.h>
  21#include <linux/mman.h>
  22#include <linux/pm.h>
  23#include <linux/syscalls.h>
  24#include <linux/uaccess.h>
  25#include <linux/smp.h>
  26#include <asm/core_reg.h>
  27#include <asm/user_gateway.h>
  28#include <asm/tcm.h>
  29#include <asm/traps.h>
  30#include <asm/switch_to.h>
  31
  32/*
  33 * Wait for the next interrupt and enable local interrupts
  34 */
  35void arch_cpu_idle(void)
  36{
  37        int tmp;
  38
  39        /*
  40         * Quickly jump straight into the interrupt entry point without actually
  41         * triggering an interrupt. When TXSTATI gets read the processor will
  42         * block until an interrupt is triggered.
  43         */
  44        asm volatile (/* Switch into ISTAT mode */
  45                      "RTH\n\t"
  46                      /* Enable local interrupts */
  47                      "MOV      TXMASKI, %1\n\t"
  48                      /*
  49                       * We can't directly "SWAP PC, PCX", so we swap via a
  50                       * temporary. Essentially we do:
  51                       *  PCX_new = 1f (the place to continue execution)
  52                       *  PC = PCX_old
  53                       */
  54                      "ADD      %0, CPC0, #(1f-.)\n\t"
  55                      "SWAP     PCX, %0\n\t"
  56                      "MOV      PC, %0\n"
  57                      /* Continue execution here with interrupts enabled */
  58                      "1:"
  59                      : "=a" (tmp)
  60                      : "r" (get_trigger_mask()));
  61}
  62
  63#ifdef CONFIG_HOTPLUG_CPU
  64void arch_cpu_idle_dead(void)
  65{
  66        cpu_die();
  67}
  68#endif
  69
  70void (*pm_power_off)(void);
  71EXPORT_SYMBOL(pm_power_off);
  72
  73void (*soc_restart)(char *cmd);
  74void (*soc_halt)(void);
  75
  76void machine_restart(char *cmd)
  77{
  78        if (soc_restart)
  79                soc_restart(cmd);
  80        hard_processor_halt(HALT_OK);
  81}
  82
  83void machine_halt(void)
  84{
  85        if (soc_halt)
  86                soc_halt();
  87        smp_send_stop();
  88        hard_processor_halt(HALT_OK);
  89}
  90
  91void machine_power_off(void)
  92{
  93        if (pm_power_off)
  94                pm_power_off();
  95        smp_send_stop();
  96        hard_processor_halt(HALT_OK);
  97}
  98
  99#define FLAG_Z 0x8
 100#define FLAG_N 0x4
 101#define FLAG_O 0x2
 102#define FLAG_C 0x1
 103
 104void show_regs(struct pt_regs *regs)
 105{
 106        int i;
 107        const char *AX0_names[] = {"A0StP", "A0FrP"};
 108        const char *AX1_names[] = {"A1GbP", "A1LbP"};
 109
 110        const char *DX0_names[] = {
 111                "D0Re0",
 112                "D0Ar6",
 113                "D0Ar4",
 114                "D0Ar2",
 115                "D0FrT",
 116                "D0.5 ",
 117                "D0.6 ",
 118                "D0.7 "
 119        };
 120
 121        const char *DX1_names[] = {
 122                "D1Re0",
 123                "D1Ar5",
 124                "D1Ar3",
 125                "D1Ar1",
 126                "D1RtP",
 127                "D1.5 ",
 128                "D1.6 ",
 129                "D1.7 "
 130        };
 131
 132        show_regs_print_info(KERN_INFO);
 133
 134        pr_info(" pt_regs @ %p\n", regs);
 135        pr_info(" SaveMask = 0x%04hx\n", regs->ctx.SaveMask);
 136        pr_info(" Flags = 0x%04hx (%c%c%c%c)\n", regs->ctx.Flags,
 137                regs->ctx.Flags & FLAG_Z ? 'Z' : 'z',
 138                regs->ctx.Flags & FLAG_N ? 'N' : 'n',
 139                regs->ctx.Flags & FLAG_O ? 'O' : 'o',
 140                regs->ctx.Flags & FLAG_C ? 'C' : 'c');
 141        pr_info(" TXRPT = 0x%08x\n", regs->ctx.CurrRPT);
 142        pr_info(" PC = 0x%08x\n", regs->ctx.CurrPC);
 143
 144        /* AX regs */
 145        for (i = 0; i < 2; i++) {
 146                pr_info(" %s = 0x%08x    ",
 147                        AX0_names[i],
 148                        regs->ctx.AX[i].U0);
 149                printk(" %s = 0x%08x\n",
 150                        AX1_names[i],
 151                        regs->ctx.AX[i].U1);
 152        }
 153
 154        if (regs->ctx.SaveMask & TBICTX_XEXT_BIT)
 155                pr_warn(" Extended state present - AX2.[01] will be WRONG\n");
 156
 157        /* Special place with AXx.2 */
 158        pr_info(" A0.2  = 0x%08x    ",
 159                regs->ctx.Ext.AX2.U0);
 160        printk(" A1.2  = 0x%08x\n",
 161                regs->ctx.Ext.AX2.U1);
 162
 163        /* 'extended' AX regs (nominally, just AXx.3) */
 164        for (i = 0; i < (TBICTX_AX_REGS - 3); i++) {
 165                pr_info(" A0.%d  = 0x%08x    ", i + 3, regs->ctx.AX3[i].U0);
 166                printk(" A1.%d  = 0x%08x\n", i + 3, regs->ctx.AX3[i].U1);
 167        }
 168
 169        for (i = 0; i < 8; i++) {
 170                pr_info(" %s = 0x%08x    ", DX0_names[i], regs->ctx.DX[i].U0);
 171                printk(" %s = 0x%08x\n", DX1_names[i], regs->ctx.DX[i].U1);
 172        }
 173
 174        show_trace(NULL, (unsigned long *)regs->ctx.AX[0].U0, regs);
 175}
 176
 177int copy_thread(unsigned long clone_flags, unsigned long usp,
 178                unsigned long arg, struct task_struct *tsk)
 179{
 180        struct pt_regs *childregs = task_pt_regs(tsk);
 181        void *kernel_context = ((void *) childregs +
 182                                sizeof(struct pt_regs));
 183        unsigned long global_base;
 184
 185        BUG_ON(((unsigned long)childregs) & 0x7);
 186        BUG_ON(((unsigned long)kernel_context) & 0x7);
 187
 188        memset(&tsk->thread.kernel_context, 0,
 189                        sizeof(tsk->thread.kernel_context));
 190
 191        tsk->thread.kernel_context = __TBISwitchInit(kernel_context,
 192                                                     ret_from_fork,
 193                                                     0, 0);
 194
 195        if (unlikely(tsk->flags & PF_KTHREAD)) {
 196                /*
 197                 * Make sure we don't leak any kernel data to child's regs
 198                 * if kernel thread becomes a userspace thread in the future
 199                 */
 200                memset(childregs, 0 , sizeof(struct pt_regs));
 201
 202                global_base = __core_reg_get(A1GbP);
 203                childregs->ctx.AX[0].U1 = (unsigned long) global_base;
 204                childregs->ctx.AX[0].U0 = (unsigned long) kernel_context;
 205                /* Set D1Ar1=arg and D1RtP=usp (fn) */
 206                childregs->ctx.DX[4].U1 = usp;
 207                childregs->ctx.DX[3].U1 = arg;
 208                tsk->thread.int_depth = 2;
 209                return 0;
 210        }
 211        /*
 212         * Get a pointer to where the new child's register block should have
 213         * been pushed.
 214         * The Meta's stack grows upwards, and the context is the the first
 215         * thing to be pushed by TBX (phew)
 216         */
 217        *childregs = *current_pt_regs();
 218        /* Set the correct stack for the clone mode */
 219        if (usp)
 220                childregs->ctx.AX[0].U0 = ALIGN(usp, 8);
 221        tsk->thread.int_depth = 1;
 222
 223        /* set return value for child process */
 224        childregs->ctx.DX[0].U0 = 0;
 225
 226        /* The TLS pointer is passed as an argument to sys_clone. */
 227        if (clone_flags & CLONE_SETTLS)
 228                tsk->thread.tls_ptr =
 229                                (__force void __user *)childregs->ctx.DX[1].U1;
 230
 231#ifdef CONFIG_METAG_FPU
 232        if (tsk->thread.fpu_context) {
 233                struct meta_fpu_context *ctx;
 234
 235                ctx = kmemdup(tsk->thread.fpu_context,
 236                              sizeof(struct meta_fpu_context), GFP_ATOMIC);
 237                tsk->thread.fpu_context = ctx;
 238        }
 239#endif
 240
 241#ifdef CONFIG_METAG_DSP
 242        if (tsk->thread.dsp_context) {
 243                struct meta_ext_context *ctx;
 244                int i;
 245
 246                ctx = kmemdup(tsk->thread.dsp_context,
 247                              sizeof(struct meta_ext_context), GFP_ATOMIC);
 248                for (i = 0; i < 2; i++)
 249                        ctx->ram[i] = kmemdup(ctx->ram[i], ctx->ram_sz[i],
 250                                              GFP_ATOMIC);
 251                tsk->thread.dsp_context = ctx;
 252        }
 253#endif
 254
 255        return 0;
 256}
 257
 258#ifdef CONFIG_METAG_FPU
 259static void alloc_fpu_context(struct thread_struct *thread)
 260{
 261        thread->fpu_context = kzalloc(sizeof(struct meta_fpu_context),
 262                                      GFP_ATOMIC);
 263}
 264
 265static void clear_fpu(struct thread_struct *thread)
 266{
 267        thread->user_flags &= ~TBICTX_FPAC_BIT;
 268        kfree(thread->fpu_context);
 269        thread->fpu_context = NULL;
 270}
 271#else
 272static void clear_fpu(struct thread_struct *thread)
 273{
 274}
 275#endif
 276
 277#ifdef CONFIG_METAG_DSP
 278static void clear_dsp(struct thread_struct *thread)
 279{
 280        if (thread->dsp_context) {
 281                kfree(thread->dsp_context->ram[0]);
 282                kfree(thread->dsp_context->ram[1]);
 283
 284                kfree(thread->dsp_context);
 285
 286                thread->dsp_context = NULL;
 287        }
 288
 289        __core_reg_set(D0.8, 0);
 290}
 291#else
 292static void clear_dsp(struct thread_struct *thread)
 293{
 294}
 295#endif
 296
 297struct task_struct *__sched __switch_to(struct task_struct *prev,
 298                                        struct task_struct *next)
 299{
 300        TBIRES to, from;
 301
 302        to.Switch.pCtx = next->thread.kernel_context;
 303        to.Switch.pPara = prev;
 304
 305#ifdef CONFIG_METAG_FPU
 306        if (prev->thread.user_flags & TBICTX_FPAC_BIT) {
 307                struct pt_regs *regs = task_pt_regs(prev);
 308                TBIRES state;
 309
 310                state.Sig.SaveMask = prev->thread.user_flags;
 311                state.Sig.pCtx = &regs->ctx;
 312
 313                if (!prev->thread.fpu_context)
 314                        alloc_fpu_context(&prev->thread);
 315                if (prev->thread.fpu_context)
 316                        __TBICtxFPUSave(state, prev->thread.fpu_context);
 317        }
 318        /*
 319         * Force a restore of the FPU context next time this process is
 320         * scheduled.
 321         */
 322        if (prev->thread.fpu_context)
 323                prev->thread.fpu_context->needs_restore = true;
 324#endif
 325
 326
 327        from = __TBISwitch(to, &prev->thread.kernel_context);
 328
 329        /* Restore TLS pointer for this process. */
 330        set_gateway_tls(current->thread.tls_ptr);
 331
 332        return (struct task_struct *) from.Switch.pPara;
 333}
 334
 335void flush_thread(void)
 336{
 337        clear_fpu(&current->thread);
 338        clear_dsp(&current->thread);
 339}
 340
 341/*
 342 * Free current thread data structures etc.
 343 */
 344void exit_thread(void)
 345{
 346        clear_fpu(&current->thread);
 347        clear_dsp(&current->thread);
 348}
 349
 350/* TODO: figure out how to unwind the kernel stack here to figure out
 351 * where we went to sleep. */
 352unsigned long get_wchan(struct task_struct *p)
 353{
 354        return 0;
 355}
 356
 357int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
 358{
 359        /* Returning 0 indicates that the FPU state was not stored (as it was
 360         * not in use) */
 361        return 0;
 362}
 363
 364#ifdef CONFIG_METAG_USER_TCM
 365
 366#define ELF_MIN_ALIGN   PAGE_SIZE
 367
 368#define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
 369#define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
 370#define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
 371
 372#define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
 373
 374unsigned long __metag_elf_map(struct file *filep, unsigned long addr,
 375                              struct elf_phdr *eppnt, int prot, int type,
 376                              unsigned long total_size)
 377{
 378        unsigned long map_addr, size;
 379        unsigned long page_off = ELF_PAGEOFFSET(eppnt->p_vaddr);
 380        unsigned long raw_size = eppnt->p_filesz + page_off;
 381        unsigned long off = eppnt->p_offset - page_off;
 382        unsigned int tcm_tag;
 383        addr = ELF_PAGESTART(addr);
 384        size = ELF_PAGEALIGN(raw_size);
 385
 386        /* mmap() will return -EINVAL if given a zero size, but a
 387         * segment with zero filesize is perfectly valid */
 388        if (!size)
 389                return addr;
 390
 391        tcm_tag = tcm_lookup_tag(addr);
 392
 393        if (tcm_tag != TCM_INVALID_TAG)
 394                type &= ~MAP_FIXED;
 395
 396        /*
 397        * total_size is the size of the ELF (interpreter) image.
 398        * The _first_ mmap needs to know the full size, otherwise
 399        * randomization might put this image into an overlapping
 400        * position with the ELF binary image. (since size < total_size)
 401        * So we first map the 'big' image - and unmap the remainder at
 402        * the end. (which unmap is needed for ELF images with holes.)
 403        */
 404        if (total_size) {
 405                total_size = ELF_PAGEALIGN(total_size);
 406                map_addr = vm_mmap(filep, addr, total_size, prot, type, off);
 407                if (!BAD_ADDR(map_addr))
 408                        vm_munmap(map_addr+size, total_size-size);
 409        } else
 410                map_addr = vm_mmap(filep, addr, size, prot, type, off);
 411
 412        if (!BAD_ADDR(map_addr) && tcm_tag != TCM_INVALID_TAG) {
 413                struct tcm_allocation *tcm;
 414                unsigned long tcm_addr;
 415
 416                tcm = kmalloc(sizeof(*tcm), GFP_KERNEL);
 417                if (!tcm)
 418                        return -ENOMEM;
 419
 420                tcm_addr = tcm_alloc(tcm_tag, raw_size);
 421                if (tcm_addr != addr) {
 422                        kfree(tcm);
 423                        return -ENOMEM;
 424                }
 425
 426                tcm->tag = tcm_tag;
 427                tcm->addr = tcm_addr;
 428                tcm->size = raw_size;
 429
 430                list_add(&tcm->list, &current->mm->context.tcm);
 431
 432                eppnt->p_vaddr = map_addr;
 433                if (copy_from_user((void *) addr, (void __user *) map_addr,
 434                                   raw_size))
 435                        return -EFAULT;
 436        }
 437
 438        return map_addr;
 439}
 440#endif
 441