The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/mips/mips/stack_machdep.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2005 Antoine Brodin
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD$");
   31 
   32 #include <sys/param.h>
   33 #include <sys/systm.h>
   34 #include <sys/lock.h>
   35 #include <sys/mutex.h>
   36 #include <sys/proc.h>
   37 #include <sys/stack.h>
   38 
   39 #include <machine/mips_opcode.h>
   40 
   41 #include <machine/pcb.h>
   42 #include <machine/regnum.h>
   43 
   44 #define VALID_PC(addr)          ((addr) >= (uintptr_t)btext && (addr) % 4 == 0)
   45 
   46 static void
   47 stack_capture(struct stack *st, struct thread *td, uintptr_t pc, uintptr_t sp)
   48 {
   49         u_register_t ra;
   50         uintptr_t i, ra_addr;
   51         int ra_stack_pos, stacksize;
   52         InstFmt insn;
   53 
   54         stack_zero(st);
   55 
   56         for (;;) {
   57                 if (!VALID_PC(pc))
   58                         break;
   59 
   60                 /*
   61                  * Walk backward from the PC looking for the function
   62                  * start.  Assume a subtraction from SP is the start
   63                  * of a function.  Hope that we find the store of RA
   64                  * into the stack frame along the way and save the
   65                  * offset of the saved RA relative to SP.
   66                  */
   67                 ra_stack_pos = -1;
   68                 stacksize = 0;
   69                 for (i = pc; VALID_PC(i); i -= sizeof(insn)) {
   70                         bcopy((void *)i, &insn, sizeof(insn));
   71                         switch (insn.IType.op) {
   72                         case OP_ADDI:
   73                         case OP_ADDIU:
   74                         case OP_DADDI:
   75                         case OP_DADDIU:
   76                                 if (insn.IType.rs != SP || insn.IType.rt != SP)
   77                                         break;
   78 
   79                                 /*
   80                                  * Ignore stack fixups in "early"
   81                                  * returns in a function, or if the
   82                                  * call was from an unlikely branch
   83                                  * moved after the end of the normal
   84                                  * return.
   85                                  */
   86                                 if ((short)insn.IType.imm > 0)
   87                                         break;
   88 
   89                                 stacksize = -(short)insn.IType.imm;
   90                                 break;
   91 
   92                         case OP_SW:
   93                         case OP_SD:
   94                                 if (insn.IType.rs != SP || insn.IType.rt != RA)
   95                                         break;
   96                                 ra_stack_pos = (short)insn.IType.imm;
   97                                 break;
   98                         default:
   99                                 break;
  100                         }
  101 
  102                         if (stacksize != 0)
  103                                 break;
  104                 }
  105 
  106                 if (stack_put(st, pc) == -1)
  107                         break;
  108 
  109                 if (ra_stack_pos == -1)
  110                         break;
  111 
  112                 /*
  113                  * Walk forward from the PC to find the function end
  114                  * (jr RA).  If eret is hit instead, stop unwinding.
  115                  */
  116                 ra_addr = sp + ra_stack_pos;
  117                 ra = 0;
  118                 for (i = pc; VALID_PC(i); i += sizeof(insn)) {
  119                         bcopy((void *)i, &insn, sizeof(insn));
  120 
  121                         switch (insn.IType.op) {
  122                         case OP_SPECIAL:
  123                                 if (insn.RType.func == OP_JR) {
  124                                         if (insn.RType.rs != RA)
  125                                                 break;
  126                                         if (!kstack_contains(td, ra_addr,
  127                                             sizeof(ra)))
  128                                                 goto done;
  129                                         ra = *(u_register_t *)ra_addr;
  130                                         if (ra == 0)
  131                                                 goto done;
  132                                         ra -= 8;
  133                                 }
  134                                 break;
  135                         default:
  136                                 break;
  137                         }
  138 
  139                         /* eret */
  140                         if (insn.word == 0x42000018)
  141                                 goto done;
  142 
  143                         if (ra != 0)
  144                                 break;
  145                 }
  146 
  147                 if (pc == ra && stacksize == 0)
  148                         break;
  149 
  150                 sp += stacksize;
  151                 pc = ra;
  152         }
  153 done:
  154         return;
  155 }
  156 
  157 int
  158 stack_save_td(struct stack *st, struct thread *td)
  159 {
  160         uintptr_t pc, sp;
  161 
  162         THREAD_LOCK_ASSERT(td, MA_OWNED);
  163         KASSERT(!TD_IS_SWAPPED(td),
  164             ("stack_save_td: thread %p is swapped", td));
  165 
  166         if (TD_IS_RUNNING(td))
  167                 return (EOPNOTSUPP);
  168 
  169         pc = td->td_pcb->pcb_context[PCB_REG_RA];
  170         sp = td->td_pcb->pcb_context[PCB_REG_SP];
  171         stack_capture(st, td, pc, sp);
  172         return (0);
  173 }
  174 
  175 void
  176 stack_save(struct stack *st)
  177 {
  178         uintptr_t pc, sp;
  179 
  180         pc = (uintptr_t)&&here;
  181         sp = (uintptr_t)__builtin_frame_address(0);
  182 here:
  183         stack_capture(st, curthread, pc, sp);
  184 }

Cache object: 800c7e4e4c3d9f46b7753bbf3f191b30


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.