1 /*
2  *  RISC-V register structures
3  *
4  *  Copyright (c) 2019 Mark Corbin
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef TARGET_ARCH_REG_H
21 #define TARGET_ARCH_REG_H
22 
23 /* Compare with riscv/include/reg.h */
24 typedef struct target_reg {
25     uint64_t ra;            /* return address */
26     uint64_t sp;            /* stack pointer */
27     uint64_t gp;            /* global pointer */
28     uint64_t tp;            /* thread pointer */
29     uint64_t t[7];          /* temporaries */
30     uint64_t s[12];         /* saved registers */
31     uint64_t a[8];          /* function arguments */
32     uint64_t sepc;          /* exception program counter */
33     uint64_t sstatus;       /* status register */
34 } target_reg_t;
35 
36 typedef struct target_fpreg {
37     uint64_t        fp_x[32][2];    /* Floating point registers */
38     uint64_t        fp_fcsr;        /* Floating point control reg */
39 } target_fpreg_t;
40 
41 #define tswapreg(ptr)   tswapal(ptr)
42 
43 /* Compare with struct trapframe in riscv/include/frame.h */
target_copy_regs(target_reg_t * regs,const CPURISCVState * env)44 static inline void target_copy_regs(target_reg_t *regs,
45                                     const CPURISCVState *env)
46 {
47 
48     regs->ra = tswapreg(env->gpr[1]);
49     regs->sp = tswapreg(env->gpr[2]);
50     regs->gp = tswapreg(env->gpr[3]);
51     regs->tp = tswapreg(env->gpr[4]);
52 
53     regs->t[0] = tswapreg(env->gpr[5]);
54     regs->t[1] = tswapreg(env->gpr[6]);
55     regs->t[2] = tswapreg(env->gpr[7]);
56     regs->t[3] = tswapreg(env->gpr[28]);
57     regs->t[4] = tswapreg(env->gpr[29]);
58     regs->t[5] = tswapreg(env->gpr[30]);
59     regs->t[6] = tswapreg(env->gpr[31]);
60 
61     regs->s[0] = tswapreg(env->gpr[8]);
62     regs->s[1] = tswapreg(env->gpr[9]);
63     regs->s[2] = tswapreg(env->gpr[18]);
64     regs->s[3] = tswapreg(env->gpr[19]);
65     regs->s[4] = tswapreg(env->gpr[20]);
66     regs->s[5] = tswapreg(env->gpr[21]);
67     regs->s[6] = tswapreg(env->gpr[22]);
68     regs->s[7] = tswapreg(env->gpr[23]);
69     regs->s[8] = tswapreg(env->gpr[24]);
70     regs->s[9] = tswapreg(env->gpr[25]);
71     regs->s[10] = tswapreg(env->gpr[26]);
72     regs->s[11] = tswapreg(env->gpr[27]);
73 
74     regs->a[0] = tswapreg(env->gpr[10]);
75     regs->a[1] = tswapreg(env->gpr[11]);
76     regs->a[2] = tswapreg(env->gpr[12]);
77     regs->a[3] = tswapreg(env->gpr[13]);
78     regs->a[4] = tswapreg(env->gpr[14]);
79     regs->a[5] = tswapreg(env->gpr[15]);
80     regs->a[6] = tswapreg(env->gpr[16]);
81     regs->a[7] = tswapreg(env->gpr[17]);
82 
83     regs->sepc = tswapreg(env->pc);
84 }
85 
86 #undef tswapreg
87 
88 #endif /* TARGET_ARCH_REG_H */
89