Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _KSTACK_H
0003 #define _KSTACK_H
0004 
0005 #include <linux/thread_info.h>
0006 #include <linux/sched.h>
0007 #include <asm/ptrace.h>
0008 #include <asm/irq.h>
0009 
0010 /* SP must be STACK_BIAS adjusted already.  */
0011 static inline bool kstack_valid(struct thread_info *tp, unsigned long sp)
0012 {
0013     unsigned long base = (unsigned long) tp;
0014 
0015     /* Stack pointer must be 16-byte aligned.  */
0016     if (sp & (16UL - 1))
0017         return false;
0018 
0019     if (sp >= (base + sizeof(struct thread_info)) &&
0020         sp <= (base + THREAD_SIZE - sizeof(struct sparc_stackf)))
0021         return true;
0022 
0023     if (hardirq_stack[tp->cpu]) {
0024         base = (unsigned long) hardirq_stack[tp->cpu];
0025         if (sp >= base &&
0026             sp <= (base + THREAD_SIZE - sizeof(struct sparc_stackf)))
0027             return true;
0028         base = (unsigned long) softirq_stack[tp->cpu];
0029         if (sp >= base &&
0030             sp <= (base + THREAD_SIZE - sizeof(struct sparc_stackf)))
0031             return true;
0032     }
0033     return false;
0034 }
0035 
0036 /* Does "regs" point to a valid pt_regs trap frame?  */
0037 static inline bool kstack_is_trap_frame(struct thread_info *tp, struct pt_regs *regs)
0038 {
0039     unsigned long base = (unsigned long) tp;
0040     unsigned long addr = (unsigned long) regs;
0041 
0042     if (addr >= base &&
0043         addr <= (base + THREAD_SIZE - sizeof(*regs)))
0044         goto check_magic;
0045 
0046     if (hardirq_stack[tp->cpu]) {
0047         base = (unsigned long) hardirq_stack[tp->cpu];
0048         if (addr >= base &&
0049             addr <= (base + THREAD_SIZE - sizeof(*regs)))
0050             goto check_magic;
0051         base = (unsigned long) softirq_stack[tp->cpu];
0052         if (addr >= base &&
0053             addr <= (base + THREAD_SIZE - sizeof(*regs)))
0054             goto check_magic;
0055     }
0056     return false;
0057 
0058 check_magic:
0059     if ((regs->magic & ~0x1ff) == PT_REGS_MAGIC)
0060         return true;
0061     return false;
0062 
0063 }
0064 
0065 static inline __attribute__((always_inline)) void *set_hardirq_stack(void)
0066 {
0067     void *orig_sp, *sp = hardirq_stack[smp_processor_id()];
0068 
0069     __asm__ __volatile__("mov %%sp, %0" : "=r" (orig_sp));
0070     if (orig_sp < sp ||
0071         orig_sp > (sp + THREAD_SIZE)) {
0072         sp += THREAD_SIZE - 192 - STACK_BIAS;
0073         __asm__ __volatile__("mov %0, %%sp" : : "r" (sp));
0074     }
0075 
0076     return orig_sp;
0077 }
0078 
0079 static inline __attribute__((always_inline)) void restore_hardirq_stack(void *orig_sp)
0080 {
0081     __asm__ __volatile__("mov %0, %%sp" : : "r" (orig_sp));
0082 }
0083 
0084 #endif /* _KSTACK_H */