Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implementation of cpu_switchto(9) for the LatticeMico32 architecture

  • Loading branch information...
commit 0a6713fb5a7ab3003f6e263389bf056f25f32930 1 parent aa55d25
Yann Sionneau authored August 30, 2013
18  sys/arch/lm32/include/asm.h
@@ -54,6 +54,14 @@
54 54
 #endif
55 55
 #define	_ASM_LABEL(x)	x
56 56
 
  57
+#ifdef __STDC__
  58
+# define __CONCAT(x,y)	x ## y
  59
+# define __STRING(x)	#x
  60
+#else
  61
+# define __CONCAT(x,y)	x/**/y
  62
+# define __STRING(x)	"x"
  63
+#endif
  64
+
57 65
 /* let kernels and others override entrypoint alignment */
58 66
 #ifndef _ALIGN_TEXT
59 67
 # define _ALIGN_TEXT .align 2
@@ -92,7 +100,15 @@
92 100
 #define	NENTRY(y)	_ENTRY(_C_LABEL(y))
93 101
 #define	ASENTRY(y)	_ENTRY(_ASM_LABEL(y)) _PROF_PROLOGUE
94 102
 
95  
-#define CPUVAR(off) _C_LABEL(cpu_info_store)+__CONCAT(CPU_INFO_,off)
  103
+#define SET_CPUVAR(off,reg) \
  104
+	mvhi	r25, hi(_C_LABEL(cpu_info_store)) ; \
  105
+	ori	r25, r25, lo(_C_LABEL(cpu_info_store)) ; \
  106
+	lw	reg, (r25+__CONCAT(CPU_INFO_,off))
  107
+
  108
+#define GET_CPUVAR(reg,off) \
  109
+	mvhi	r25, hi(_C_LABEL(cpu_info_store)) ; \
  110
+	ori	r25, r25, lo(_C_LABEL(cpu_info_store)) ; \
  111
+	sw	(r25+__CONCAT(CPU_INFO_,off)), reg
96 112
 
97 113
 #define SET_ENTRY_SIZE(y) \
98 114
 	.size	_C_LABEL(y), . - _C_LABEL(y)
2  sys/arch/lm32/include/pcb.h
@@ -39,6 +39,8 @@
39 39
 struct pcb {
40 40
 	struct pmap *pcb_pm;	/* pmap of our vmspace */
41 41
 	register_t pcb_sp;	/* saved SP */
  42
+	register_t pcb_ra;	/* saved RA */
  43
+	register_t pcb_fp;	/* saved FP */
42 44
 	int pcb_flags;
43 45
 };
44 46
 
80  sys/arch/lm32/lm32/locore_subr.S
@@ -3,6 +3,7 @@
3 3
  */
4 4
 
5 5
 #include <lm32/asm.h>
  6
+#include "assym.h"
6 7
 
7 8
 // TODO: implement suword
8 9
 ENTRY(suword)
@@ -27,3 +28,82 @@ ENTRY(setjmp)
27 28
 
28 29
 // TODO: implement longjmp
29 30
 ENTRY(longjmp)
  31
+
  32
+/*
  33
+ * struct lwp *cpu_switchto(struct lwp *oldlwp, struct *newlwp,
  34
+ *			    bool returning)
  35
+ *
  36
+ *	1. if (oldlwp != NULL), save its context.
  37
+ *	2. then, restore context of newlwp.
  38
+ *
  39
+ * Note that the stack frame layout is known to "struct switchframe" in
  40
+ * <machine/frame.h> and to the code in cpu_lwp_fork() which initializes
  41
+ * it for a new lwp.
  42
+ * r1: oldlwp and return value
  43
+ * r2: newlwp
  44
+ * r3: returning
  45
+ */
  46
+ENTRY(cpu_switchto)
  47
+	addi 	sp, sp, -16
  48
+	sw	(sp+0), fp
  49
+	sw	(sp+4), ra
  50
+	sw	(sp+8), r11
  51
+	sw	(sp+12), r12
  52
+	ori	fp, r0, 16
  53
+	add	fp, sp, sp
  54
+
  55
+	/* Save old context. */
  56
+	lw	r11, (r1+L_PCB)
  57
+	sw	(r11+PCB_SP), sp
  58
+	sw	(r11+PCB_FP), fp
  59
+	sw	(r11+PCB_RA), ra
  60
+
  61
+	/* Switch to newlwp's stack. */
  62
+1:	lw	r12, (r2+L_PCB)
  63
+	lw	ra, (r12+PCB_RA)
  64
+	lw	fp, (r12+PCB_FP)
  65
+	lw	sp, (r12+PCB_SP)
  66
+
  67
+	/* Set curlwp. */
  68
+	SET_CPUVAR(CURLWP,r12)
  69
+
  70
+	/* Skip the rest if returning to a pinned LWP. */
  71
+	bne	r3, r0, 4f /* branch if r3 != 0 */
  72
+
  73
+	/* Check if this process is using RAS (restartable atomic sequence). */
  74
+	/* TODO: check for RAS */
  75
+	lw	r12, (r2+L_PROC)
  76
+	lw	r12, (r12+P_RASLIST)
  77
+	bne	r12, r0, 5f
  78
+	
  79
+	/* Return to the new LWP, returning 'oldlwp' in r1. */
  80
+4:
  81
+	lw	r12, (sp+12)
  82
+	lw	r11, (sp+8)
  83
+	lw	ra, (sp+4)
  84
+	lw	fp, (sp+0)
  85
+	addi	sp, sp, 16
  86
+	ret
  87
+
  88
+	/* Check for restartable atomic sequence (RAS). */
  89
+5:
  90
+	sw	(sp+0), r1
  91
+	sw	(sp+4), r2
  92
+	addi	sp, sp, 8
  93
+	lw	r2, (r2+(L_MD_REGS+MD_UTF+TF_REGS+R_PC))
  94
+/*	lw	r2, (r2+MD_UTF)
  95
+	lw	r2, (r2+TF_REGS)
  96
+	lw	r2, (r2+R_PC)*/
  97
+	lw	r1, (sp+4)
  98
+	lw	r1, (r1+L_PROC)
  99
+
  100
+	/* call ras_lookup(newlwp->l_proc, newlwp.md_regs.md_utf.tf_regs.r_pc); */
  101
+	calli	_C_LABEL(ras_lookup)
  102
+
  103
+	/* save return value in tf_regs.r_pc */
  104
+	sw	(r2+R_PC), r1
  105
+
  106
+	lw	r2, (sp+4)
  107
+	lw	r1, (sp+0)
  108
+	addi	sp, sp, -8
  109
+	bi	4b
16  sys/arch/milkymist/milkymist/genassym.cf
@@ -35,8 +35,10 @@
35 35
 include <machine/pte.h>
36 36
 include <machine/vmparam.h>
37 37
 include <machine/types.h>
  38
+include <machine/pcb.h>
38 39
 include <sys/cpu.h>
39 40
 #include <sys/lwp.h>
  41
+include <sys/proc.h>
40 42
 
41 43
 # general constants
42 44
 define	PAGE_SIZE	PAGE_SIZE
@@ -51,6 +53,20 @@ define	PG_FRAME	PG_FRAME
51 53
 define	CPU_INFO_SELF		offsetof(struct cpu_info, ci_self)
52 54
 define	CPU_INFO_CURLWP		offsetof(struct cpu_info, ci_curlwp)
53 55
 
  56
+define	PCB_RA			offsetof(struct pcb, pcb_ra)
  57
+define	PCB_FP			offsetof(struct pcb, pcb_fp)
  58
+define	PCB_SP			offsetof(struct pcb, pcb_sp)
54 59
 
55 60
 define	L_NOPREEMPT		offsetof(struct lwp, l_nopreempt)
56 61
 define	L_DOPREEMPT		offsetof(struct lwp, l_dopreempt)
  62
+define	L_PCB			offsetof(struct lwp, l_addr)
  63
+define	L_MD_REGS		offsetof(struct lwp, l_md)
  64
+define	L_PROC			offsetof(struct lwp, l_proc)
  65
+
  66
+define	MD_UTF			offsetof(struct mdlwp, md_utf)
  67
+
  68
+define	TF_REGS			offsetof(struct trapframe, tf_regs)
  69
+
  70
+define	R_PC			offsetof(struct reg, r_pc)
  71
+
  72
+define	P_RASLIST		offsetof(struct proc, p_raslist)

0 notes on commit 0a6713f

Please sign in to comment.
Something went wrong with that request. Please try again.