1/*
2 * Copyright (c) 2020, 2021 Antony Pavlov <antonynpavlov@gmail.com>
3 *
4 * based on arch/riscv/core/swap.S
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9#include <zephyr/toolchain.h>
10#include <offsets_short.h>
11#include <zephyr/arch/cpu.h>
12#include <mips/regdef.h>
13
14/*
15 * unsigned int arch_swap(unsigned int key)
16 *
17 * Always called with interrupts locked
18 * key is stored in a0 register
19 */
20
21GTEXT(arch_swap)
22SECTION_FUNC(exception.other, arch_swap)
23
24	/* Make a system call to perform context switch */
25	syscall
26
27	/*
28	 * when thread is rescheduled, unlock irq and return.
29	 * Restored register v0 contains IRQ lock state of thread.
30	 */
31	la k0, _kernel
32
33	/* Get pointer to _kernel.current */
34	lw k1, _kernel_offset_to_current(k0)
35
36	/* Load return value of arch_swap function in register v0 */
37	lw v0, _thread_offset_to_swap_return_value(k1)
38
39	/*
40	 * Unlock irq, following IRQ lock state in v0 register.
41	 */
42	mfc0 k0, CP0_STATUS
43	or k0, k0, a0
44	mtc0 k0, CP0_STATUS
45	ehb
46
47	/* Return */
48	jr ra
49