/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <types.h>30#include <lib.h>31#include <thread.h>32#include <threadprivate.h>3334#include "switchframe.h"3536/* in threadstart.S */37extern void mips_threadstart(/* arguments are in unusual registers */);383940/*41* Function to initialize the switchframe of a new thread, which is42* *not* the one that is currently running.43*44* The new thread should, when it is run the first time, end up calling45* thread_startup(entrypoint, data1, data2).46*47* We arrange for this by creating a phony switchframe for48* switchframe_switch() to switch to. The only trouble is that the49* switchframe doesn't include the argument registers a0-a3. So we50* store the arguments in the s* registers, and use a bit of asm51* (mips_threadstart) to move them and then jump to thread_startup.52*/53void54switchframe_init(struct thread *thread,55void (*entrypoint)(void *data1, unsigned long data2),56void *data1, unsigned long data2)57{58vaddr_t stacktop;59struct switchframe *sf;6061/*62* MIPS stacks grow down. t_stack is just a hunk of memory, so63* get the other end of it. Then set up a switchframe on the64* top of the stack.65*/66stacktop = ((vaddr_t)thread->t_stack) + STACK_SIZE;67sf = ((struct switchframe *) stacktop) - 1;6869/* Zero out the switchframe. */70bzero(sf, sizeof(*sf));7172/*73* Now set the important parts: pass through the three arguments,74* and set the return address register to the place we want75* execution to begin.76*77* Thus, when switchframe_switch does its "j ra", it will78* actually jump to mips_threadstart, which will move the79* arguments into the right register and jump to80* thread_startup().81*82* Note that this means that when we call switchframe_switch()83* in thread_switch(), we may not come back out the same way84* in the next thread. (Though we will come back out the same85* way when we later come back to the same thread again.)86*87* This has implications for code at the bottom of88* thread_switch, described in thread.c.89*/90sf->sf_s0 = (uint32_t)entrypoint;91sf->sf_s1 = (uint32_t)data1;92sf->sf_s2 = (uint32_t)data2;93sf->sf_ra = (uint32_t)mips_threadstart;9495/* Set ->t_context, and we're done. */96thread->t_context = sf;97}9899100