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