processor.h 8.0 KB

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