summaryrefslogtreecommitdiff
path: root/libcpu/mips/x1000/stack.c
blob: 0043ea2a2185e13c6c9f033106592ff6b3e59b39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * File      : stack.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2008 - 2012, RT-Thread Development Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2016��9��8��     Urey         the first version
 */

#include <rtthread.h>

#include "../common/mips.h"

register U32 $GP __asm__ ("$28");

rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter, rt_uint8_t *stack_addr, void *texit)
{
    static rt_uint32_t wSR=0;
    static rt_uint32_t wGP;

	mips_reg_ctx	*regCtx;
	mips_arg_ctx	*argCtx;
	rt_uint32_t i;

	if (wSR == 0)
	{
		wSR = read_c0_status();
		wSR &= 0xfffffffe;
		wSR |= 0x0403;

		wGP = $GP;
	}

	if ((rt_uint32_t) stack_addr & 0x7)
	{
		stack_addr = (rt_uint8_t *)((rt_uint32_t)stack_addr - 4);
	}

	argCtx = (mips_arg_ctx *)((rt_uint32_t)stack_addr - sizeof(mips_arg_ctx));
	regCtx = (mips_reg_ctx *)((rt_uint32_t)stack_addr - sizeof(mips_arg_ctx) - sizeof(mips_reg_ctx));

	for (i = 0; i < 4; ++i)
	{
		argCtx->args[i] = i;
	}

	//����ͨ�üĴ���
	for (i = 0; i < 32; ++i)
	{
		regCtx->regs[i] = i;
	}

	regCtx->regs[REG_SP] = (rt_uint32_t)stack_addr;
	regCtx->regs[REG_A0] = (rt_uint32_t)parameter;
	regCtx->regs[REG_GP] = (rt_uint32_t)wGP;
	regCtx->regs[REG_FP] = (rt_uint32_t)0x0;
	regCtx->regs[REG_RA] = (rt_uint32_t)texit;

    regCtx->CP0DataLO	= 0x00;
    regCtx->CP0DataHI	= 0x00;
    regCtx->CP0Cause	= read_c0_cause();
    regCtx->CP0Status	= wSR;
    regCtx->CP0EPC		= (rt_uint32_t)tentry;
    regCtx->CP0BadVAddr= 0x00;

	return (rt_uint8_t *)(regCtx);
}