PageRenderTime 41ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/runtime/ppc-linux-os.c

http://github.com/sbcl/sbcl
C | 187 lines | 115 code | 19 blank | 53 comment | 0 complexity | e853822ebefc62607ee24dab41806fa6 MD5 | raw file
Possible License(s): CC0-1.0
  1. /*
  2. * This is the IBM/Motorola/Apple/whoever Linux incarnation of
  3. * arch-dependent OS-dependent routines. See also "linux-os.c". */
  4. /*
  5. * This software is part of the SBCL system. See the README file for
  6. * more information.
  7. *
  8. * This software is derived from the CMU CL system, which was
  9. * written at Carnegie Mellon University and released into the
  10. * public domain. The software is in the public domain and is
  11. * provided with absolutely no warranty. See the COPYING and CREDITS
  12. * files for more information.
  13. */
  14. /* These header files were lifted wholesale from linux-os.c, some may
  15. * be redundant. -- Dan Barlow ca. 2001-05-01 */
  16. #include <stdio.h>
  17. #include <sys/param.h>
  18. #include <sys/file.h>
  19. #include "sbcl.h"
  20. #include "./signal.h"
  21. #include "os.h"
  22. #include "arch.h"
  23. #include "globals.h"
  24. #include "interrupt.h"
  25. #include "interr.h"
  26. #include "lispregs.h"
  27. #include <sys/socket.h>
  28. #include <sys/utsname.h>
  29. #include <sys/types.h>
  30. #include <signal.h>
  31. #include <sys/time.h>
  32. #include <sys/stat.h>
  33. #include <unistd.h>
  34. #include <sys/prctl.h>
  35. #include "validate.h"
  36. #include "ppc-linux-mcontext.h"
  37. int arch_os_thread_init(struct thread *thread) {
  38. #if defined(LISP_FEATURE_SB_THREAD)
  39. pthread_setspecific(specials,thread);
  40. #endif
  41. /* For some reason, PPC Linux appears to default to not generating
  42. * floating point exceptions. PR_SET_FPEXC is a PPC-specific
  43. * option new in kernel 2.4.21 and 2.5.32 that allows us to
  44. * configure this. Should we need to run on an older kenel, the
  45. * equivalent trick is to get into a signal-handling context and
  46. * modify the saved machine state register.
  47. *
  48. * PR_FP_EXC_PRECISE may be more accurate than we need,
  49. * particularly if we move to the x86oid trick of inserting
  50. * explicit synchronization for floating-point exception
  51. * delivery. If we wish to move to such a model, the other two
  52. * exception delivery modes that we could use are PR_FP_EXC_ASYNC
  53. * and PR_FP_EXC_NONRECOV, and exception delivery can be forced
  54. * by any access to the FPSCR. -- AB, 2010-May-23 */
  55. prctl(PR_SET_FPEXC, PR_FP_EXC_PRECISE, 0, 0);
  56. return 1; /* success */
  57. }
  58. int arch_os_thread_cleanup(struct thread *thread) {
  59. return 1; /* success */
  60. }
  61. os_context_register_t *
  62. os_context_register_addr(os_context_t *context, int offset)
  63. {
  64. #if defined(GLIBC231_STYLE_UCONTEXT)
  65. return &((context->uc_mcontext.regs)->gpr[offset]);
  66. #elif defined(GLIBC232_STYLE_UCONTEXT)
  67. return &((context->uc_mcontext.uc_regs->gregs)[offset]);
  68. #endif
  69. }
  70. os_context_register_t *
  71. os_context_pc_addr(os_context_t *context)
  72. {
  73. #if defined(GLIBC231_STYLE_UCONTEXT)
  74. return &((context->uc_mcontext.regs)->nip);
  75. #elif defined(GLIBC232_STYLE_UCONTEXT)
  76. return &((context->uc_mcontext.uc_regs->gregs)[PT_NIP]);
  77. #endif
  78. }
  79. os_context_register_t *
  80. os_context_lr_addr(os_context_t *context)
  81. {
  82. #if defined(GLIBC231_STYLE_UCONTEXT)
  83. return &((context->uc_mcontext.regs)->link);
  84. #elif defined(GLIBC232_STYLE_UCONTEXT)
  85. return &((context->uc_mcontext.uc_regs->gregs)[PT_LNK]);
  86. #endif
  87. }
  88. os_context_register_t *
  89. os_context_ctr_addr(os_context_t *context)
  90. {
  91. /* Like os_context_fp_control() and os_context_lr_addr(), this
  92. * uses an index beyond the declared end of the array in order to
  93. * find the correct register value in the context. */
  94. #if defined(GLIBC231_STYLE_UCONTEXT)
  95. /* FIXME: This probably should be ->ctr instead of ->gpr[PT_CTR]. */
  96. return &((context->uc_mcontext.regs)->gpr[PT_CTR]);
  97. #elif defined(GLIBC232_STYLE_UCONTEXT)
  98. return &((context->uc_mcontext.uc_regs)->gregs[PT_CTR]);
  99. #endif
  100. }
  101. os_context_register_t *
  102. os_context_cr_addr(os_context_t *context)
  103. {
  104. /* Like os_context_fp_control() and os_context_lr_addr(), this
  105. * uses an index beyond the declared end of the array in order to
  106. * find the correct register value in the context. */
  107. #if defined(GLIBC231_STYLE_UCONTEXT)
  108. /* FIXME: This probably should be ->ccr instead of ->gpr[PT_CCR]. */
  109. return &((context->uc_mcontext.regs)->gpr[PT_CCR]);
  110. #elif defined(GLIBC232_STYLE_UCONTEXT)
  111. return &((context->uc_mcontext.uc_regs)->gregs[PT_CCR]);
  112. #endif
  113. }
  114. sigset_t *
  115. os_context_sigmask_addr(os_context_t *context)
  116. {
  117. #if defined(GLIBC231_STYLE_UCONTEXT)
  118. return &context->uc_sigmask;
  119. #elif defined(GLIBC232_STYLE_UCONTEXT)
  120. return &context->uc_sigmask;
  121. #endif
  122. }
  123. unsigned long
  124. os_context_fp_control(os_context_t *context)
  125. {
  126. /* So this may look like nice, well behaved code. However, closer
  127. inspection reveals that gpr is simply the general purpose
  128. registers, and PT_FPSCR is an offset that is larger than 32
  129. (the number of ppc registers), but that happens to get the
  130. right answer. -- CSR, 2002-07-11 */
  131. #if defined(GLIBC231_STYLE_UCONTEXT)
  132. return context->uc_mcontext.regs->gpr[PT_FPSCR];
  133. #elif defined(GLIBC232_STYLE_UCONTEXT)
  134. return context->uc_mcontext.uc_regs->gregs[PT_FPSCR];
  135. #endif
  136. }
  137. void
  138. os_restore_fp_control(os_context_t *context)
  139. {
  140. /* KLUDGE: mtfsf has to be run against a float register, so we
  141. * construct the float we need to use as an integer, then cast
  142. * a pointer to its storage to a double and load that. For
  143. * this to work, control must be the same width as a double,
  144. * 64 bits. And why aren't we using a union here, anyway? */
  145. unsigned long long control;
  146. double d;
  147. /* FIXME: We are only preserving enabled traps and rounding
  148. * mode here. Do we also want to preserve "fast mode"? */
  149. control = os_context_fp_control(context) &
  150. (FLOAT_TRAPS_BYTE_MASK | FLOAT_ROUNDING_MODE_MASK);
  151. d = *((double *) &control);
  152. asm volatile ("mtfsf 0xff,%0" : : "f" (d));
  153. }
  154. void
  155. os_flush_icache(os_vm_address_t address, os_vm_size_t length)
  156. {
  157. /* see ppc-arch.c */
  158. ppc_flush_icache(address,length);
  159. }
  160. // "cc -S" on this file shows that the C compiler is responsible for making
  161. // a substitution for these functions with an additional argument in front.
  162. // I don't want to know how to do that from Lisp.
  163. #include <sys/types.h>
  164. #include <sys/stat.h>
  165. #include <unistd.h>
  166. int _stat(const char *pathname, struct stat *sb) {return stat(pathname, sb); }
  167. int _lstat(const char *pathname, struct stat *sb) { return lstat(pathname, sb); }
  168. int _fstat(int fd, struct stat *sb) { return fstat(fd, sb); }