processor.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2001 - 2008 Tensilica Inc.
  7. * Copyright (C) 2015 Cadence Design Systems Inc.
  8. */
  9. #ifndef _XTENSA_PROCESSOR_H
  10. #define _XTENSA_PROCESSOR_H
  11. #include <asm/core.h>
  12. #include <linux/compiler.h>
  13. #include <linux/stringify.h>
  14. #include <asm/bootparam.h>
  15. #include <asm/ptrace.h>
  16. #include <asm/types.h>
  17. #include <asm/regs.h>
  18. #define ARCH_SLAB_MINALIGN XTENSA_STACK_ALIGNMENT
  19. /*
  20. * User space process size: 1 GB.
  21. * Windowed call ABI requires caller and callee to be located within the same
  22. * 1 GB region. The C compiler places trampoline code on the stack for sources
  23. * that take the address of a nested C function (a feature used by glibc), so
  24. * the 1 GB requirement applies to the stack as well.
  25. */
  26. #ifdef CONFIG_MMU
  27. #define TASK_SIZE __XTENSA_UL_CONST(0x40000000)
  28. #else
  29. #define TASK_SIZE __XTENSA_UL_CONST(0xffffffff)
  30. #endif
  31. #define STACK_TOP TASK_SIZE
  32. #define STACK_TOP_MAX STACK_TOP
  33. /*
  34. * General exception cause assigned to fake NMI. Fake NMI needs to be handled
  35. * differently from other interrupts, but it uses common kernel entry/exit
  36. * code.
  37. */
  38. #define EXCCAUSE_MAPPED_NMI 62
  39. /*
  40. * General exception cause assigned to debug exceptions. Debug exceptions go
  41. * to their own vector, rather than the general exception vectors (user,
  42. * kernel, double); and their specific causes are reported via DEBUGCAUSE
  43. * rather than EXCCAUSE. However it is sometimes convenient to redirect debug
  44. * exceptions to the general exception mechanism. To do this, an otherwise
  45. * unused EXCCAUSE value was assigned to debug exceptions for this purpose.
  46. */
  47. #define EXCCAUSE_MAPPED_DEBUG 63
  48. /*
  49. * We use DEPC also as a flag to distinguish between double and regular
  50. * exceptions. For performance reasons, DEPC might contain the value of
  51. * EXCCAUSE for regular exceptions, so we use this definition to mark a
  52. * valid double exception address.
  53. * (Note: We use it in bgeui, so it should be 64, 128, or 256)
  54. */
  55. #define VALID_DOUBLE_EXCEPTION_ADDRESS 64
  56. #define XTENSA_INT_LEVEL(intno) _XTENSA_INT_LEVEL(intno)
  57. #define _XTENSA_INT_LEVEL(intno) XCHAL_INT##intno##_LEVEL
  58. #define XTENSA_INTLEVEL_MASK(level) _XTENSA_INTLEVEL_MASK(level)
  59. #define _XTENSA_INTLEVEL_MASK(level) (XCHAL_INTLEVEL##level##_MASK)
  60. #define XTENSA_INTLEVEL_ANDBELOW_MASK(l) _XTENSA_INTLEVEL_ANDBELOW_MASK(l)
  61. #define _XTENSA_INTLEVEL_ANDBELOW_MASK(l) (XCHAL_INTLEVEL##l##_ANDBELOW_MASK)
  62. #define PROFILING_INTLEVEL XTENSA_INT_LEVEL(XCHAL_PROFILING_INTERRUPT)
  63. /* LOCKLEVEL defines the interrupt level that masks all
  64. * general-purpose interrupts.
  65. */
  66. #if defined(CONFIG_XTENSA_FAKE_NMI) && defined(XCHAL_PROFILING_INTERRUPT)
  67. #define LOCKLEVEL (PROFILING_INTLEVEL - 1)
  68. #else
  69. #define LOCKLEVEL XCHAL_EXCM_LEVEL
  70. #endif
  71. #define TOPLEVEL XCHAL_EXCM_LEVEL
  72. #define XTENSA_FAKE_NMI (LOCKLEVEL < TOPLEVEL)
  73. /* WSBITS and WBBITS are the width of the WINDOWSTART and WINDOWBASE
  74. * registers
  75. */
  76. #define WSBITS (XCHAL_NUM_AREGS / 4) /* width of WINDOWSTART in bits */
  77. #define WBBITS (XCHAL_NUM_AREGS_LOG2 - 2) /* width of WINDOWBASE in bits */
  78. #if defined(__XTENSA_WINDOWED_ABI__)
  79. #define KERNEL_PS_WOE_MASK PS_WOE_MASK
  80. #elif defined(__XTENSA_CALL0_ABI__)
  81. #define KERNEL_PS_WOE_MASK 0
  82. #else
  83. #error Unsupported xtensa ABI
  84. #endif
  85. #ifndef __ASSEMBLER__
  86. #if defined(__XTENSA_WINDOWED_ABI__)
  87. /* Build a valid return address for the specified call winsize.
  88. * winsize must be 1 (call4), 2 (call8), or 3 (call12)
  89. */
  90. #define MAKE_RA_FOR_CALL(ra,ws) (((ra) & 0x3fffffff) | (ws) << 30)
  91. /* Convert return address to a valid pc
  92. * Note: 'text' is the address within the same 1GB range as the ra
  93. */
  94. #define MAKE_PC_FROM_RA(ra, text) (((ra) & 0x3fffffff) | ((unsigned long)(text) & 0xc0000000))
  95. #elif defined(__XTENSA_CALL0_ABI__)
  96. /* Build a valid return address for the specified call winsize.
  97. * winsize must be 1 (call4), 2 (call8), or 3 (call12)
  98. */
  99. #define MAKE_RA_FOR_CALL(ra, ws) (ra)
  100. /* Convert return address to a valid pc
  101. * Note: 'text' is not used as 'ra' is always the full address
  102. */
  103. #define MAKE_PC_FROM_RA(ra, text) (ra)
  104. #else
  105. #error Unsupported Xtensa ABI
  106. #endif
  107. /* Spill slot location for the register reg in the spill area under the stack
  108. * pointer sp. reg must be in the range [0..4).
  109. */
  110. #define SPILL_SLOT(sp, reg) (*(((unsigned long *)(sp)) - 4 + (reg)))
  111. /* Spill slot location for the register reg in the spill area under the stack
  112. * pointer sp for the call8. reg must be in the range [4..8).
  113. */
  114. #define SPILL_SLOT_CALL8(sp, reg) (*(((unsigned long *)(sp)) - 12 + (reg)))
  115. /* Spill slot location for the register reg in the spill area under the stack
  116. * pointer sp for the call12. reg must be in the range [4..12).
  117. */
  118. #define SPILL_SLOT_CALL12(sp, reg) (*(((unsigned long *)(sp)) - 16 + (reg)))
  119. struct thread_struct {
  120. /* kernel's return address and stack pointer for context switching */
  121. unsigned long ra; /* kernel's a0: return address and window call size */
  122. unsigned long sp; /* kernel's a1: stack pointer */
  123. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  124. struct perf_event *ptrace_bp[XCHAL_NUM_IBREAK];
  125. struct perf_event *ptrace_wp[XCHAL_NUM_DBREAK];
  126. #endif
  127. } __aligned(16);
  128. /* This decides where the kernel will search for a free chunk of vm
  129. * space during mmap's.
  130. */
  131. #define TASK_UNMAPPED_BASE (TASK_SIZE / 2)
  132. #define INIT_THREAD \
  133. { \
  134. ra: 0, \
  135. sp: sizeof(init_stack) + (long) &init_stack, \
  136. }
  137. /*
  138. * Do necessary setup to start up a newly executed thread.
  139. * Note: When windowed ABI is used for userspace we set-up ps
  140. * as if we did a call4 to the new pc.
  141. * set_thread_state in signal.c depends on it.
  142. */
  143. #if IS_ENABLED(CONFIG_USER_ABI_CALL0)
  144. #define USER_PS_VALUE ((USER_RING << PS_RING_SHIFT) | \
  145. (1 << PS_UM_BIT) | \
  146. (1 << PS_EXCM_BIT))
  147. #else
  148. #define USER_PS_VALUE (PS_WOE_MASK | \
  149. (1 << PS_CALLINC_SHIFT) | \
  150. (USER_RING << PS_RING_SHIFT) | \
  151. (1 << PS_UM_BIT) | \
  152. (1 << PS_EXCM_BIT))
  153. #endif
  154. /* Clearing a0 terminates the backtrace. */
  155. #define start_thread(regs, new_pc, new_sp) \
  156. do { \
  157. unsigned long syscall = (regs)->syscall; \
  158. unsigned long current_aregs[16]; \
  159. memcpy(current_aregs, (regs)->areg, sizeof(current_aregs)); \
  160. memset((regs), 0, sizeof(*(regs))); \
  161. (regs)->pc = (new_pc); \
  162. (regs)->ps = USER_PS_VALUE; \
  163. memcpy((regs)->areg, current_aregs, sizeof(current_aregs)); \
  164. (regs)->areg[1] = (new_sp); \
  165. (regs)->areg[0] = 0; \
  166. (regs)->wmask = 1; \
  167. (regs)->depc = 0; \
  168. (regs)->windowbase = 0; \
  169. (regs)->windowstart = 1; \
  170. (regs)->syscall = syscall; \
  171. } while (0)
  172. /* Forward declaration */
  173. struct task_struct;
  174. struct mm_struct;
  175. extern unsigned long __get_wchan(struct task_struct *p);
  176. void init_arch(bp_tag_t *bp_start);
  177. void do_notify_resume(struct pt_regs *regs);
  178. #define KSTK_EIP(tsk) (task_pt_regs(tsk)->pc)
  179. #define KSTK_ESP(tsk) (task_pt_regs(tsk)->areg[1])
  180. #define cpu_relax() barrier()
  181. /* Special register access. */
  182. #define xtensa_set_sr(x, sr) \
  183. ({ \
  184. __asm__ __volatile__ ("wsr %0, "__stringify(sr) :: \
  185. "a"((unsigned int)(x))); \
  186. })
  187. #define xtensa_get_sr(sr) \
  188. ({ \
  189. unsigned int v; \
  190. __asm__ __volatile__ ("rsr %0, "__stringify(sr) : "=a"(v)); \
  191. v; \
  192. })
  193. #define xtensa_xsr(x, sr) \
  194. ({ \
  195. unsigned int __v__ = (unsigned int)(x); \
  196. __asm__ __volatile__ ("xsr %0, " __stringify(sr) : "+a"(__v__)); \
  197. __v__; \
  198. })
  199. #if XCHAL_HAVE_EXTERN_REGS
  200. static inline void set_er(unsigned long value, unsigned long addr)
  201. {
  202. asm volatile ("wer %0, %1" : : "a" (value), "a" (addr) : "memory");
  203. }
  204. static inline unsigned long get_er(unsigned long addr)
  205. {
  206. register unsigned long value;
  207. asm volatile ("rer %0, %1" : "=a" (value) : "a" (addr) : "memory");
  208. return value;
  209. }
  210. #endif /* XCHAL_HAVE_EXTERN_REGS */
  211. #endif /* __ASSEMBLER__ */
  212. #endif /* _XTENSA_PROCESSOR_H */