Skip to contentSkip to conversation

microblaze: Add support for the clz insn

PATCH In review Edgar E. Iglesias 3 participants 9 messages Jan 10, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 microblaze: Add support for the clz insn

---
 target-microblaze/helper.h    |    1 +
 target-microblaze/op_helper.c |   11 +++++++++++
 target-microblaze/translate.c |   11 +++++++++++
 3 files changed, 23 insertions(+), 0 deletions(-)
target-microblaze/helper.h +1 -0
index b92aa34..420ff3b 100644
@@ -5,6 +5,7 @@ DEF_HELPER_0(debug, void)
 DEF_HELPER_FLAGS_3(carry, TCG_CALL_PURE | TCG_CALL_CONST, i32, i32, i32, i32)
 DEF_HELPER_2(cmp, i32, i32, i32)
 DEF_HELPER_2(cmpu, i32, i32, i32)
+DEF_HELPER_1(clz, i32, i32)
 
 DEF_HELPER_2(divs, i32, i32, i32)
 DEF_HELPER_2(divu, i32, i32, i32)
target-microblaze/op_helper.c +11 -0
index 7433cec..fe38b7b 100644
@@ -165,6 +165,17 @@ uint32_t helper_cmpu(uint32_t a, uint32_t b)
     return t;
 }
 
+uint32_t helper_clz(uint32_t t0)
+{
+    if (t0 == 0) {
+        return 32;
+    }
+    if (t0 == ~0) {
+        return 0;
+    }
+    return clz32(t0);
+}
+
 uint32_t helper_carry(uint32_t a, uint32_t b, uint32_t cf)
 {
     uint32_t ncf;
target-microblaze/translate.c +11 -0
index d7f513d..f4e6f30 100644
@@ -809,6 +809,17 @@ static void dec_bit(DisasContext *dc)
                 return;
             }
             break;
+        case 0xe0:
+            if ((dc->tb_flags & MSR_EE_FLAG)
+                && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
+                && !((dc->env->pvr.regs[2] & PVR2_USE_PCMP_INSTR))) {
+                tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
+                t_gen_raise_exception(dc, EXCP_HW_EXCP);
+            }
+            if (dc->env->pvr.regs[2] & PVR2_USE_PCMP_INSTR) {
+                gen_helper_clz(cpu_R[dc->rd], cpu_R[dc->ra]);
+            }
+            break;
         default:
             cpu_abort(dc->env, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
                      dc->pc, op, dc->rd, dc->ra, dc->rb);
Signature · 1 line
1.7.3.4
#1 lore raw

PATCH 1/1 etraxfs-dma: Model metadata and eop

From: Lars Persson <[email protected]>

- Send EOP flags to the out channels.
- Send data descriptor metadata to the out channels.

---
 hw/etraxfs_dma.c |   28 ++++++++++++++++++++++------
 hw/etraxfs_dma.h |   13 ++++++++++---
 hw/etraxfs_eth.c |    2 +-
 3 files changed, 33 insertions(+), 10 deletions(-)
hw/etraxfs_dma.c +22 -6
index d2bd584..332525c 100644
@@ -401,15 +401,29 @@ static int channel_out_run(struct fs_dma_ctrl *ctrl, int c)
 	uint32_t saved_data_buf;
 	unsigned char buf[2 * 1024];
 
+	struct dma_context_metadata meta;
+	bool send_context = true;
+
 	if (ctrl->channels[c].eol)
 		return 0;
 
 	do {
+		bool out_eop;
 		D(printf("ch=%d buf=%x after=%x\n",
 			 c,
 			 (uint32_t)ctrl->channels[c].current_d.buf,
 			 (uint32_t)ctrl->channels[c].current_d.after));
 
+		if (send_context) {
+			if (ctrl->channels[c].client->client.metadata_push) {
+				meta.metadata = ctrl->channels[c].current_d.md;
+				ctrl->channels[c].client->client.metadata_push(
+					ctrl->channels[c].client->client.opaque,
+					&meta);
+			}
+			send_context = false;
+		}
+
 		channel_load_d(ctrl, c);
 		saved_data_buf = channel_reg(ctrl, c, RW_SAVED_DATA_BUF);
 		len = (uint32_t)(unsigned long)
@@ -420,13 +434,17 @@ static int channel_out_run(struct fs_dma_ctrl *ctrl, int c)
 			len = sizeof buf;
 		cpu_physical_memory_read (saved_data_buf, buf, len);
 
-		D(printf("channel %d pushes %x %u bytes\n", c, 
-			 saved_data_buf, len));
+		out_eop = ((saved_data_buf + len) ==
+		           ctrl->channels[c].current_d.after) &&
+			ctrl->channels[c].current_d.out_eop;
+
+		D(printf("channel %d pushes %x %u bytes eop=%u\n", c,
+		         saved_data_buf, len, out_eop));
 
 		if (ctrl->channels[c].client->client.push)
 			ctrl->channels[c].client->client.push(
 				ctrl->channels[c].client->client.opaque,
-				buf, len);
+				buf, len, out_eop);
 		else
 			printf("WARNING: DMA ch%d dataloss,"
 			       " no attached client.\n", c);
@@ -437,11 +455,9 @@ static int channel_out_run(struct fs_dma_ctrl *ctrl, int c)
 				ctrl->channels[c].current_d.after) {
 			/* Done. Step to next.  */
 			if (ctrl->channels[c].current_d.out_eop) {
-				/* TODO: signal eop to the client.  */
-				D(printf("signal eop\n"));
+				send_context = true;
 			}
 			if (ctrl->channels[c].current_d.intr) {
-				/* TODO: signal eop to the client.  */
 				/* data intr.  */
 				D(printf("signal intr %d eol=%d\n",
 					len, ctrl->channels[c].current_d.eol));
hw/etraxfs_dma.h +10 -3
index 96408ab..021c52a 100644
@@ -1,3 +1,8 @@
+struct dma_context_metadata {
+	/* data descriptor md */
+	uint16_t metadata;
+};
+
 struct etraxfs_dma_client
 {
 	/* DMA controller. */
@@ -5,10 +10,12 @@ struct etraxfs_dma_client
 	void *ctrl;
 
 	/* client.  */
-	struct
-	{
-		int (*push)(void *opaque, unsigned char *buf, int len);
+	struct {
+		int (*push)(void *opaque, unsigned char *buf,
+		            int len, bool eop);
 		void (*pull)(void *opaque);
+		void (*metadata_push)(void *opaque,
+		                      const struct dma_context_metadata *md);
 		void *opaque;
 	} client;
 };
hw/etraxfs_eth.c +1 -1
index b525258..5afa55f 100644
@@ -540,7 +540,7 @@ static ssize_t eth_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
         return size;
 }
 
-static int eth_tx_push(void *opaque, unsigned char *buf, int len)
+static int eth_tx_push(void *opaque, unsigned char *buf, int len, bool eop)
 {
 	struct fs_eth *eth = opaque;
 
Signature · 1 line
1.7.3.4
#2 lore raw

Re: [Qemu-devel] [PATCH] etraxfs-dma: Model metadata and eop

Forgot to CC Lars on this one.

Also note that this patch has tab issues due to the ETRAX DMA model beeing written prior to CodingStyle.

Cheers

Quoted 127 lines · Edgar E. Iglesias

From: Lars Persson <[email protected]>

- Send EOP flags to the out channels.
- Send data descriptor metadata to the out channels.

---
 hw/etraxfs_dma.c |   28 ++++++++++++++++++++++------
 hw/etraxfs_dma.h |   13 ++++++++++---
 hw/etraxfs_eth.c |    2 +-
 3 files changed, 33 insertions(+), 10 deletions(-)
hw/etraxfs_dma.c +22 -6
index d2bd584..332525c 100644
@@ -401,15 +401,29 @@ static int channel_out_run(struct fs_dma_ctrl *ctrl, int c)
 	uint32_t saved_data_buf;
 	unsigned char buf[2 * 1024];
 
+	struct dma_context_metadata meta;
+	bool send_context = true;
+
 	if (ctrl->channels[c].eol)
 		return 0;
 
 	do {
+		bool out_eop;
 		D(printf("ch=%d buf=%x after=%x\n",
 			 c,
 			 (uint32_t)ctrl->channels[c].current_d.buf,
 			 (uint32_t)ctrl->channels[c].current_d.after));
 
+		if (send_context) {
+			if (ctrl->channels[c].client->client.metadata_push) {
+				meta.metadata = ctrl->channels[c].current_d.md;
+				ctrl->channels[c].client->client.metadata_push(
+					ctrl->channels[c].client->client.opaque,
+					&meta);
+			}
+			send_context = false;
+		}
+
 		channel_load_d(ctrl, c);
 		saved_data_buf = channel_reg(ctrl, c, RW_SAVED_DATA_BUF);
 		len = (uint32_t)(unsigned long)
@@ -420,13 +434,17 @@ static int channel_out_run(struct fs_dma_ctrl *ctrl, int c)
 			len = sizeof buf;
 		cpu_physical_memory_read (saved_data_buf, buf, len);
 
-		D(printf("channel %d pushes %x %u bytes\n", c, 
-			 saved_data_buf, len));
+		out_eop = ((saved_data_buf + len) ==
+		           ctrl->channels[c].current_d.after) &&
+			ctrl->channels[c].current_d.out_eop;
+
+		D(printf("channel %d pushes %x %u bytes eop=%u\n", c,
+		         saved_data_buf, len, out_eop));
 
 		if (ctrl->channels[c].client->client.push)
 			ctrl->channels[c].client->client.push(
 				ctrl->channels[c].client->client.opaque,
-				buf, len);
+				buf, len, out_eop);
 		else
 			printf("WARNING: DMA ch%d dataloss,"
 			       " no attached client.\n", c);
@@ -437,11 +455,9 @@ static int channel_out_run(struct fs_dma_ctrl *ctrl, int c)
 				ctrl->channels[c].current_d.after) {
 			/* Done. Step to next.  */
 			if (ctrl->channels[c].current_d.out_eop) {
-				/* TODO: signal eop to the client.  */
-				D(printf("signal eop\n"));
+				send_context = true;
 			}
 			if (ctrl->channels[c].current_d.intr) {
-				/* TODO: signal eop to the client.  */
 				/* data intr.  */
 				D(printf("signal intr %d eol=%d\n",
 					len, ctrl->channels[c].current_d.eol));
hw/etraxfs_dma.h +10 -3
index 96408ab..021c52a 100644
@@ -1,3 +1,8 @@
+struct dma_context_metadata {
+	/* data descriptor md */
+	uint16_t metadata;
+};
+
 struct etraxfs_dma_client
 {
 	/* DMA controller. */
@@ -5,10 +10,12 @@ struct etraxfs_dma_client
 	void *ctrl;
 
 	/* client.  */
-	struct
-	{
-		int (*push)(void *opaque, unsigned char *buf, int len);
+	struct {
+		int (*push)(void *opaque, unsigned char *buf,
+		            int len, bool eop);
 		void (*pull)(void *opaque);
+		void (*metadata_push)(void *opaque,
+		                      const struct dma_context_metadata *md);
 		void *opaque;
 	} client;
 };
hw/etraxfs_eth.c +1 -1
index b525258..5afa55f 100644
@@ -540,7 +540,7 @@ static ssize_t eth_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
         return size;
 }
 
-static int eth_tx_push(void *opaque, unsigned char *buf, int len)
+static int eth_tx_push(void *opaque, unsigned char *buf, int len, bool eop)
 {
 	struct fs_eth *eth = opaque;
 
Signature · 1 line
1.7.3.4
#3 lore raw

PATCH 1/1 microblaze: Break the tb at memory barriers

In reply to Edgar E. Iglesias
---
 target-microblaze/translate.c |   12 +++++++++++-
 1 files changed, 11 insertions(+), 1 deletions(-)
target-microblaze/translate.c +11 -1
index 180ac84..96ce2ec 100644
@@ -1249,12 +1249,22 @@ static void dec_bcc(DisasContext *dc)
 
 static void dec_br(DisasContext *dc)
 {
-    unsigned int dslot, link, abs;
+    unsigned int dslot, link, abs, mbar;
     int mem_index = cpu_mmu_index(dc->env);
 
     dslot = dc->ir & (1 << 20);
     abs = dc->ir & (1 << 19);
     link = dc->ir & (1 << 18);
+
+    /* Memory barrier.  */
+    mbar = (dc->ir >> 16) & 31;
+    if (mbar == 2 && dc->imm == 4) {
+        LOG_DIS("mbar %d\n", dc->rd);
+        /* Break the TB.  */
+        dc->cpustate_changed = 1;
+        return;
+    }
+
     LOG_DIS("br%s%s%s%s imm=%x\n",
              abs ? "a" : "", link ? "l" : "",
              dc->type_b ? "i" : "", dslot ? "d" : "",
Signature · 1 line
1.7.3.4
#4 lore raw

PATCH 1/1 microblaze: Emulate the hw stackprotector

In reply to Edgar E. Iglesias
---
 target-microblaze/cpu.h       |    3 +++
 target-microblaze/helper.h    |    1 +
 target-microblaze/op_helper.c |   11 +++++++++++
 target-microblaze/translate.c |   33 +++++++++++++++++++++++++++++++++
 4 files changed, 48 insertions(+), 0 deletions(-)
target-microblaze/cpu.h +3 -0
index 1a307e3..3ecaeee 100644
@@ -93,6 +93,7 @@ struct CPUMBState;
 #define          ESR_EC_DIVZERO         5
 #define          ESR_EC_FPU             6
 #define          ESR_EC_PRIVINSN        7
+#define          ESR_EC_STACKPROT       7  /* Same as PRIVINSN.  */
 #define          ESR_EC_DATA_STORAGE    8
 #define          ESR_EC_INSN_STORAGE    9
 #define          ESR_EC_DATA_TLB        10
@@ -235,6 +236,8 @@ typedef struct CPUMBState {
     uint32_t regs[33];
     uint32_t sregs[24];
     float_status fp_status;
+    /* Stack protectors. Yes, it's a hw feature.  */
+    uint32_t slr, shr;
 
     /* Internal flags.  */
 #define IMM_FLAG	4
target-microblaze/helper.h +1 -0
index 420ff3b..2205d5a 100644
@@ -33,6 +33,7 @@ DEF_HELPER_2(mmu_write, void, i32, i32)
 #endif
 
 DEF_HELPER_4(memalign, void, i32, i32, i32, i32)
+DEF_HELPER_1(stackprot, void, i32)
 
 DEF_HELPER_2(get, i32, i32, i32)
 DEF_HELPER_3(put, void, i32, i32, i32)
target-microblaze/op_helper.c +11 -0
index fe38b7b..5fd7906 100644
@@ -489,6 +489,17 @@ void helper_memalign(uint32_t addr, uint32_t dr, uint32_t wr, uint32_t mask)
     }
 }
 
+void helper_stackprot(uint32_t addr)
+{
+    if (addr < env->slr || addr > env->shr) {
+            qemu_log("Stack protector violation at %x %x %x\n",
+                     addr, env->slr, env->shr);
+            env->sregs[SR_EAR] = addr;
+            env->sregs[SR_ESR] = ESR_EC_STACKPROT;
+            helper_raise_exception(EXCP_HW_EXCP);
+    }
+}
+
 #if !defined(CONFIG_USER_ONLY)
 /* Writes/reads to the MMU's special regs end up here.  */
 uint32_t helper_mmu_read(uint32_t rn)
target-microblaze/translate.c +33 -0
index f4e6f30..180ac84 100644
@@ -526,6 +526,12 @@ static void dec_msr(DisasContext *dc)
             case 0x7:
                 tcg_gen_andi_tl(cpu_SR[SR_FSR], cpu_R[dc->ra], 31);
                 break;
+            case 0x800:
+                tcg_gen_st_tl(cpu_R[dc->ra], cpu_env, offsetof(CPUState, slr));
+                break;
+            case 0x802:
+                tcg_gen_st_tl(cpu_R[dc->ra], cpu_env, offsetof(CPUState, shr));
+                break;
             default:
                 cpu_abort(dc->env, "unknown mts reg %x\n", sr);
                 break;
@@ -552,6 +558,12 @@ static void dec_msr(DisasContext *dc)
             case 0xb:
                 tcg_gen_mov_tl(cpu_R[dc->rd], cpu_SR[SR_BTR]);
                 break;
+            case 0x800:
+                tcg_gen_ld_tl(cpu_R[dc->rd], cpu_env, offsetof(CPUState, slr));
+                break;
+            case 0x802:
+                tcg_gen_ld_tl(cpu_R[dc->rd], cpu_env, offsetof(CPUState, shr));
+                break;
             case 0x2000:
             case 0x2001:
             case 0x2002:
@@ -864,6 +876,13 @@ static inline void gen_load(DisasContext *dc, TCGv dst, TCGv addr,
 static inline TCGv *compute_ldst_addr(DisasContext *dc, TCGv *t)
 {
     unsigned int extimm = dc->tb_flags & IMM_FLAG;
+    /* Should be set to one if r1 is used by loadstores.  */
+    int stackprot = 0;
+
+    /* All load/stores use ra.  */
+    if (dc->ra == 1) {
+        stackprot = 1;
+    }
 
     /* Treat the common cases first.  */
     if (!dc->type_b) {
@@ -874,8 +893,16 @@ static inline TCGv *compute_ldst_addr(DisasContext *dc, TCGv *t)
             return &cpu_R[dc->ra];
         }
 
+        if (dc->rb == 1) {
+            stackprot = 1;
+        }
+
         *t = tcg_temp_new();
         tcg_gen_add_tl(*t, cpu_R[dc->ra], cpu_R[dc->rb]);
+
+        if (stackprot) {
+            gen_helper_stackprot(*t);
+        }
         return t;
     }
     /* Immediate.  */
@@ -891,6 +918,9 @@ static inline TCGv *compute_ldst_addr(DisasContext *dc, TCGv *t)
         tcg_gen_add_tl(*t, cpu_R[dc->ra], *(dec_alu_op_b(dc)));
     }
 
+    if (stackprot) {
+        gen_helper_stackprot(*t);
+    }
     return t;
 }
 
@@ -1917,6 +1947,9 @@ void cpu_reset (CPUState *env)
     memset(env, 0, offsetof(CPUMBState, breakpoints));
     tlb_flush(env, 1);
 
+    /* Disable stack protector.  */
+    env->shr = ~0;
+
     env->pvr.regs[0] = PVR0_PVR_FULL_MASK \
                        | PVR0_USE_BARREL_MASK \
                        | PVR0_USE_DIV_MASK \
Signature · 1 line
1.7.3.4
#5 lore raw
In reply to Edgar E. Iglesias

target-microblaze/op_helper.c uint32_t helper_cmpu(uint32_t a, uint32_t b) · line 177

Quoted 10 lines · Edgar E. Iglesias
target-microblaze/op_helper.c +10 -0
+uint32_t helper_clz(uint32_t t0)
+{
+    if (t0 == 0) {
+        return 32;
+    }
+    if (t0 == ~0) {
+        return 0;
+    }
+    return clz32(t0);
+}

I think clz32() handles both the 0 and 0xffffffff cases correctly, so they don't need special-casing.

-- PMM

#6 lore raw
Quoted 14 lines · Peter Maydell

target-microblaze/op_helper.c uint32_t helper_cmpu(uint32_t a, uint32_t b) · line 177

Quoted 10 lines · Edgar E. Iglesias
target-microblaze/op_helper.c +10 -0
+uint32_t helper_clz(uint32_t t0)
+{
+    if (t0 == 0) {
+        return 32;
+    }
+    if (t0 == ~0) {
+        return 0;
+    }
+    return clz32(t0);
+}

I think clz32() handles both the 0 and 0xffffffff cases correctly, so they don't need special-casing.

That seems to be the case, I'll fix that, thanks.

Cheers

#7 lore raw
In reply to Edgar E. Iglesias

target-microblaze/helper.h DEF_HELPER_0(debug, void) · line 8

Quoted 6 lines · Edgar E. Iglesias

+++ b/target-microblaze/helper.h

target-microblaze/helper.h +1 -0
@@ -5,6 +5,7 @@ DEF_HELPER_0(debug, void)
 DEF_HELPER_FLAGS_3(carry, TCG_CALL_PURE | TCG_CALL_CONST, i32, i32, i32, i32)
 DEF_HELPER_2(cmp, i32, i32, i32)
 DEF_HELPER_2(cmpu, i32, i32, i32)
+DEF_HELPER_1(clz, i32, i32)

Use DEF_HELPER_FLAGS_1 with PURE|CONST, since the function examines nothing but its arguments.

r~

#8 lore raw
Quoted 10 lines · Richard Henderson

target-microblaze/helper.h DEF_HELPER_0(debug, void) · line 8

Quoted 6 lines · Edgar E. Iglesias

+++ b/target-microblaze/helper.h

target-microblaze/helper.h +1 -0
@@ -5,6 +5,7 @@ DEF_HELPER_0(debug, void)
 DEF_HELPER_FLAGS_3(carry, TCG_CALL_PURE | TCG_CALL_CONST, i32, i32, i32, i32)
 DEF_HELPER_2(cmp, i32, i32, i32)
 DEF_HELPER_2(cmpu, i32, i32, i32)
+DEF_HELPER_1(clz, i32, i32)

Use DEF_HELPER_FLAGS_1 with PURE|CONST, since the function examines nothing but its arguments.

Yep thank, noticed that when I changed the helper today :)

Cheers