windows.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* windows.c: Routines to deal with register window management
  3. * at the C-code level.
  4. *
  5. * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/sched.h>
  9. #include <linux/string.h>
  10. #include <linux/mm.h>
  11. #include <linux/smp.h>
  12. #include <asm/cacheflush.h>
  13. #include <linux/uaccess.h>
  14. #include "kernel.h"
  15. /* Do save's until all user register windows are out of the cpu. */
  16. void flush_user_windows(void)
  17. {
  18. register int ctr asm("g5");
  19. ctr = 0;
  20. __asm__ __volatile__(
  21. "\n1:\n\t"
  22. "ld [%%g6 + %2], %%g4\n\t"
  23. "orcc %%g0, %%g4, %%g0\n\t"
  24. "add %0, 1, %0\n\t"
  25. "bne 1b\n\t"
  26. " save %%sp, -64, %%sp\n"
  27. "2:\n\t"
  28. "subcc %0, 1, %0\n\t"
  29. "bne 2b\n\t"
  30. " restore %%g0, %%g0, %%g0\n"
  31. : "=&r" (ctr)
  32. : "0" (ctr),
  33. "i" ((const unsigned long)TI_UWINMASK)
  34. : "g4", "cc");
  35. }
  36. static inline void shift_window_buffer(int first_win, int last_win, struct thread_info *tp)
  37. {
  38. int i;
  39. for(i = first_win; i < last_win; i++) {
  40. tp->rwbuf_stkptrs[i] = tp->rwbuf_stkptrs[i+1];
  41. memcpy(&tp->reg_window[i], &tp->reg_window[i+1], sizeof(struct reg_window32));
  42. }
  43. }
  44. /* Place as many of the user's current register windows
  45. * on the stack that we can. Even if the %sp is unaligned
  46. * we still copy the window there, the only case that we don't
  47. * succeed is if the %sp points to a bum mapping altogether.
  48. * setup_frame() and do_sigreturn() use this before shifting
  49. * the user stack around. Future instruction and hardware
  50. * bug workaround routines will need this functionality as
  51. * well.
  52. */
  53. void synchronize_user_stack(void)
  54. {
  55. struct thread_info *tp = current_thread_info();
  56. int window;
  57. flush_user_windows();
  58. if(!tp->w_saved)
  59. return;
  60. /* Ok, there is some dirty work to do. */
  61. for(window = tp->w_saved - 1; window >= 0; window--) {
  62. unsigned long sp = tp->rwbuf_stkptrs[window];
  63. /* Ok, let it rip. */
  64. if (copy_to_user((char __user *) sp, &tp->reg_window[window],
  65. sizeof(struct reg_window32)))
  66. continue;
  67. shift_window_buffer(window, tp->w_saved - 1, tp);
  68. tp->w_saved--;
  69. }
  70. }
  71. #if 0
  72. /* An optimization. */
  73. static inline void copy_aligned_window(void *dest, const void *src)
  74. {
  75. __asm__ __volatile__("ldd [%1], %%g2\n\t"
  76. "ldd [%1 + 0x8], %%g4\n\t"
  77. "std %%g2, [%0]\n\t"
  78. "std %%g4, [%0 + 0x8]\n\t"
  79. "ldd [%1 + 0x10], %%g2\n\t"
  80. "ldd [%1 + 0x18], %%g4\n\t"
  81. "std %%g2, [%0 + 0x10]\n\t"
  82. "std %%g4, [%0 + 0x18]\n\t"
  83. "ldd [%1 + 0x20], %%g2\n\t"
  84. "ldd [%1 + 0x28], %%g4\n\t"
  85. "std %%g2, [%0 + 0x20]\n\t"
  86. "std %%g4, [%0 + 0x28]\n\t"
  87. "ldd [%1 + 0x30], %%g2\n\t"
  88. "ldd [%1 + 0x38], %%g4\n\t"
  89. "std %%g2, [%0 + 0x30]\n\t"
  90. "std %%g4, [%0 + 0x38]\n\t" : :
  91. "r" (dest), "r" (src) :
  92. "g2", "g3", "g4", "g5");
  93. }
  94. #endif
  95. /* Try to push the windows in a threads window buffer to the
  96. * user stack. Unaligned %sp's are not allowed here.
  97. */
  98. void try_to_clear_window_buffer(struct pt_regs *regs, int who)
  99. {
  100. struct thread_info *tp = current_thread_info();
  101. int window;
  102. flush_user_windows();
  103. for(window = 0; window < tp->w_saved; window++) {
  104. unsigned long sp = tp->rwbuf_stkptrs[window];
  105. if ((sp & 7) ||
  106. copy_to_user((char __user *) sp, &tp->reg_window[window],
  107. sizeof(struct reg_window32)))
  108. do_exit(SIGILL);
  109. }
  110. tp->w_saved = 0;
  111. }