[Pdbg] [PATCH 1/9] libpdbg: Make thread procedures consistent

Amitay Isaacs amitay at ozlabs.org
Mon Jun 22 10:44:53 AEST 2020


All thread procedures should use struct thread as the first argument
instead of struct pdbg_target.

Signed-off-by: Amitay Isaacs <amitay at ozlabs.org>
---
 libpdbg/chip.c   | 79 ++++++++++++++++++++++++++++++++----------------
 libpdbg/chip.h   |  5 ++-
 libpdbg/hwunit.h |  6 ++--
 libpdbg/p8chip.c | 13 +++-----
 libpdbg/p9chip.c |  8 ++---
 src/pdbgproxy.c  |  2 +-
 6 files changed, 69 insertions(+), 44 deletions(-)

diff --git a/libpdbg/chip.c b/libpdbg/chip.c
index 86e91bd..f9f184b 100644
--- a/libpdbg/chip.c
+++ b/libpdbg/chip.c
@@ -274,18 +274,14 @@ int thread_sreset_all(void)
  * data. Note that only registers r0 and r1 are saved and restored so
  * opcode sequences must preserve other registers.
  */
-int ram_instructions(struct pdbg_target *thread_target, uint64_t *opcodes,
+int ram_instructions(struct thread *thread, uint64_t *opcodes,
 			    uint64_t *results, int len, unsigned int lpar)
 {
 	uint64_t opcode = 0, r0 = 0, r1 = 0, scratch = 0;
 	int i;
 	int exception = 0;
-	struct thread *thread;
 	bool did_setup = false;
 
-	assert(!strcmp(thread_target->class, "thread"));
-	thread = target_to_thread(thread_target);
-
 	if (!thread->ram_is_setup) {
 		CHECK_ERR(thread->ram_setup(thread));
 		did_setup = true;
@@ -339,31 +335,40 @@ int ram_instructions(struct pdbg_target *thread_target, uint64_t *opcodes,
 /*
  * Get gpr value. Chip must be stopped.
  */
-int thread_getgpr(struct pdbg_target *thread, int gpr, uint64_t *value)
+int thread_getgpr(struct pdbg_target *target, int gpr, uint64_t *value)
 {
+	struct thread *thread;
 	uint64_t opcodes[] = {mtspr(277, gpr)};
 	uint64_t results[] = {0};
 
+	assert(pdbg_target_is_class(target, "thread"));
+	thread = target_to_thread(target);
 	CHECK_ERR(ram_instructions(thread, opcodes, results, ARRAY_SIZE(opcodes), 0));
 	*value = results[0];
 	return 0;
 }
 
-int thread_putgpr(struct pdbg_target *thread, int gpr, uint64_t value)
+int thread_putgpr(struct pdbg_target *target, int gpr, uint64_t value)
 {
+	struct thread *thread;
 	uint64_t opcodes[] = {mfspr(gpr, 277)};
 	uint64_t results[] = {value};
 
+	assert(pdbg_target_is_class(target, "thread"));
+	thread = target_to_thread(target);
 	CHECK_ERR(ram_instructions(thread, opcodes, results, ARRAY_SIZE(opcodes), 0));
 
 	return 0;
 }
 
-int thread_getnia(struct pdbg_target *thread, uint64_t *value)
+int thread_getnia(struct pdbg_target *target, uint64_t *value)
 {
+	struct thread *thread;
 	uint64_t opcodes[] = {mfnia(0), mtspr(277, 0)};
 	uint64_t results[] = {0, 0};
 
+	assert(pdbg_target_is_class(target, "thread"));
+	thread = target_to_thread(target);
 	CHECK_ERR(ram_instructions(thread, opcodes, results, ARRAY_SIZE(opcodes), 0));
 	*value = results[1];
 	return 0;
@@ -376,8 +381,9 @@ int thread_getnia(struct pdbg_target *thread, uint64_t *value)
  * This is a hack and should be made much cleaner once we have target
  * specific putspr commands.
  */
-int thread_putnia(struct pdbg_target *thread, uint64_t value)
+int thread_putnia(struct pdbg_target *target, uint64_t value)
 {
+	struct thread *thread;
 	uint64_t opcodes[] = {	mfspr(1, 8),	/* mflr r1 */
 				mfspr(0, 277),	/* value -> r0 */
 				mtspr(8, 0),	/* mtlr r0 */
@@ -385,42 +391,53 @@ int thread_putnia(struct pdbg_target *thread, uint64_t value)
 				mtspr(8, 1), };	/* mtlr r1 */
 	uint64_t results[] = {0, value, 0, 0, 0};
 
+	assert(pdbg_target_is_class(target, "thread"));
+	thread = target_to_thread(target);
 	CHECK_ERR(ram_instructions(thread, opcodes, results, ARRAY_SIZE(opcodes), 0));
 	return 0;
 }
 
-int thread_getspr(struct pdbg_target *thread, int spr, uint64_t *value)
+int thread_getspr(struct pdbg_target *target, int spr, uint64_t *value)
 {
+	struct thread *thread;
 	uint64_t opcodes[] = {mfspr(0, spr), mtspr(277, 0)};
 	uint64_t results[] = {0, 0};
 
+	assert(pdbg_target_is_class(target, "thread"));
+	thread = target_to_thread(target);
 	CHECK_ERR(ram_instructions(thread, opcodes, results, ARRAY_SIZE(opcodes), 0));
 	*value = results[1];
 	return 0;
 }
 
-int thread_putspr(struct pdbg_target *thread, int spr, uint64_t value)
+int thread_putspr(struct pdbg_target *target, int spr, uint64_t value)
 {
+	struct thread *thread;
 	uint64_t opcodes[] = {mfspr(0, 277), mtspr(spr, 0)};
 	uint64_t results[] = {value, 0};
 
+	assert(pdbg_target_is_class(target, "thread"));
+	thread = target_to_thread(target);
 	CHECK_ERR(ram_instructions(thread, opcodes, results, ARRAY_SIZE(opcodes), 0));
 	return 0;
 }
 
-int thread_getmsr(struct pdbg_target *thread, uint64_t *value)
+int thread_getmsr(struct pdbg_target *target, uint64_t *value)
 {
+	struct thread *thread;
 	uint64_t opcodes[] = {mfmsr(0), mtspr(277, 0)};
 	uint64_t results[] = {0, 0};
 
+	assert(pdbg_target_is_class(target, "thread"));
+	thread = target_to_thread(target);
 	CHECK_ERR(ram_instructions(thread, opcodes, results, ARRAY_SIZE(opcodes), 0));
 	*value = results[1];
 	return 0;
 }
 
-int thread_getcr(struct pdbg_target *thread, uint32_t *value)
+int thread_getcr(struct pdbg_target *target, uint32_t *value)
 {
-
+	struct thread *thread;
 	uint64_t opcodes[] = {mfocrf(0, 0), mtspr(277, 0), mfocrf(0, 1), mtspr(277, 0),
 			      mfocrf(0, 2), mtspr(277, 0), mfocrf(0, 3), mtspr(277, 0),
 			      mfocrf(0, 4), mtspr(277, 0), mfocrf(0, 5), mtspr(277, 0),
@@ -429,6 +446,8 @@ int thread_getcr(struct pdbg_target *thread, uint32_t *value)
 	uint32_t cr_field, cr = 0;
 	int i;
 
+	assert(pdbg_target_is_class(target, "thread"));
+	thread = target_to_thread(target);
 	CHECK_ERR(ram_instructions(thread, opcodes, results, ARRAY_SIZE(opcodes), 0));
 	for (i = 1; i < 16; i += 2) {
 		cr_field = results[i];
@@ -440,58 +459,66 @@ int thread_getcr(struct pdbg_target *thread, uint32_t *value)
 	return 0;
 }
 
-int thread_putcr(struct pdbg_target *thread, uint32_t value)
+int thread_putcr(struct pdbg_target *target, uint32_t value)
 {
+	struct thread *thread;
 	uint64_t opcodes[] = {mfspr(0, 277), mtocrf(0, 0), mtocrf(1, 0),
 			      mtocrf(2, 0), mtocrf(3, 0), mtocrf(4, 0),
 			      mtocrf(5, 0), mtocrf(6, 0), mtocrf(7, 0)};
 	uint64_t results[] = {value};
 
+	assert(pdbg_target_is_class(target, "thread"));
+	thread = target_to_thread(target);
 	CHECK_ERR(ram_instructions(thread, opcodes, results, ARRAY_SIZE(opcodes), 0));
 
 	return 0;
 }
 
-int thread_putmsr(struct pdbg_target *thread, uint64_t value)
+int thread_putmsr(struct pdbg_target *target, uint64_t value)
 {
+	struct thread *thread;
 	uint64_t opcodes[] = {mfspr(0, 277), mtmsr(0)};
 	uint64_t results[] = {value, 0};
 
+	assert(pdbg_target_is_class(target, "thread"));
+	thread = target_to_thread(target);
 	CHECK_ERR(ram_instructions(thread, opcodes, results, ARRAY_SIZE(opcodes), 0));
 	return 0;
 }
 
-int thread_getmem(struct pdbg_target *thread, uint64_t addr, uint64_t *value)
+int thread_getmem(struct pdbg_target *target, uint64_t addr, uint64_t *value)
 {
+	struct thread *thread;
 	uint64_t opcodes[] = {mfspr(0, 277), mfspr(1, 277), ld(0, 0, 1), mtspr(277, 0)};
 	uint64_t results[] = {0xdeaddeaddeaddead, addr, 0, 0};
 
+	assert(pdbg_target_is_class(target, "thread"));
+	thread = target_to_thread(target);
 	CHECK_ERR(ram_instructions(thread, opcodes, results, ARRAY_SIZE(opcodes), 0));
 	*value = results[3];
 	return 0;
 }
 
-int thread_getxer(struct pdbg_target *thread_target, uint64_t *value)
+int thread_getxer(struct pdbg_target *target, uint64_t *value)
 {
-
 	struct thread *thread;
 
-	assert(!strcmp(thread_target->class, "thread"));
-	thread = target_to_thread(thread_target);
+	assert(pdbg_target_is_class(target, "thread"));
+	thread = target_to_thread(target);
 
-	CHECK_ERR(thread->ram_getxer(thread_target, value));
+	CHECK_ERR(thread->ram_getxer(thread, value));
 
 	return 0;
 }
 
-int thread_putxer(struct pdbg_target *thread_target, uint64_t value)
+int thread_putxer(struct pdbg_target *target, uint64_t value)
 {
 	struct thread *thread;
 
-	assert(!strcmp(thread_target->class, "thread"));
-	thread = target_to_thread(thread_target);
+	assert(pdbg_target_is_class(target, "thread"));
+	thread = target_to_thread(target);
 
-	CHECK_ERR(thread->ram_putxer(thread_target, value));
+	CHECK_ERR(thread->ram_putxer(thread, value));
 
 	return 0;
 }
diff --git a/libpdbg/chip.h b/libpdbg/chip.h
index 52f3486..b3342b8 100644
--- a/libpdbg/chip.h
+++ b/libpdbg/chip.h
@@ -16,9 +16,12 @@
 #ifndef __LIBPDBG_CHIP_H
 #define __LIBPDBG_CHIP_H
 
+#include "libpdbg.h"
+#include "hwunit.h"
+
 uint64_t mfspr(uint64_t reg, uint64_t spr) __attribute__ ((visibility("hidden")));
 uint64_t mtspr(uint64_t spr, uint64_t reg) __attribute__ ((visibility("hidden")));
-int ram_instructions(struct pdbg_target *thread_target, uint64_t *opcodes,
+int ram_instructions(struct thread *thread, uint64_t *opcodes,
 		uint64_t *results, int len, unsigned int lpar) __attribute__
 		((visibility("hidden")));
 #endif
diff --git a/libpdbg/hwunit.h b/libpdbg/hwunit.h
index e3a8426..7fe83a6 100644
--- a/libpdbg/hwunit.h
+++ b/libpdbg/hwunit.h
@@ -140,9 +140,9 @@ struct thread {
 	int (*ram_setup)(struct thread *);
 	int (*ram_instruction)(struct thread *, uint64_t opcode, uint64_t *scratch);
 	int (*ram_destroy)(struct thread *);
-	int (*ram_getxer)(struct pdbg_target *, uint64_t *value);
-	int (*ram_putxer)(struct pdbg_target *, uint64_t value);
-	int (*enable_attn)(struct pdbg_target *);
+	int (*ram_getxer)(struct thread *, uint64_t *value);
+	int (*ram_putxer)(struct thread *, uint64_t value);
+	int (*enable_attn)(struct thread *);
 };
 #define target_to_thread(x) container_of(x, struct thread, target)
 
diff --git a/libpdbg/p8chip.c b/libpdbg/p8chip.c
index 484d77c..4a2061a 100644
--- a/libpdbg/p8chip.c
+++ b/libpdbg/p8chip.c
@@ -477,7 +477,7 @@ static int p8_ram_destroy(struct thread *thread)
 	return 0;
 }
 
-static int p8_ram_getxer(struct pdbg_target *thread, uint64_t *value)
+static int p8_ram_getxer(struct thread *thread, uint64_t *value)
 {
 	uint64_t opcodes[] = {mfxerf(0, 0), mtspr(277, 0), mfxerf(0, 1),
 			      mtspr(277, 0), mfxerf(0, 2), mtspr(277, 0),
@@ -496,7 +496,7 @@ static int p8_ram_getxer(struct pdbg_target *thread, uint64_t *value)
 	return 0;
 }
 
-static int p8_ram_putxer(struct pdbg_target *thread, uint64_t value)
+static int p8_ram_putxer(struct thread *thread, uint64_t value)
 {
 	uint64_t fields[] = {value, value, value, value, 0};
 	uint64_t opcodes[] = {mfspr(0, 277), mtxerf(0, 0), mtxerf(0, 1), mtxerf(0, 2), mtxerf(0, 3)};
@@ -618,17 +618,12 @@ static int p8_put_hid0(struct pdbg_target *chip, uint64_t value)
 	return 0;
 }
 
-static int p8_enable_attn(struct pdbg_target *target)
+static int p8_enable_attn(struct thread *thread)
 {
 	struct pdbg_target *core;
 	uint64_t hid0;
 
-	core = pdbg_target_parent("core", target);
-	if (core == NULL)
-	{
-		PR_ERROR("CORE NOT FOUND\n");
-		return 1;
-	}
+	core = pdbg_target_require_parent("core", &thread->target);
 
 	/* Need to enable the attn instruction in HID0 */
 	if (p8_get_hid0(core, &hid0)) {
diff --git a/libpdbg/p9chip.c b/libpdbg/p9chip.c
index 6222dc3..811bca6 100644
--- a/libpdbg/p9chip.c
+++ b/libpdbg/p9chip.c
@@ -404,16 +404,16 @@ static int p9_ram_destroy(struct thread *thread)
 	return 0;
 }
 
-static int p9_ram_getxer(struct pdbg_target *thread, uint64_t *value)
+static int p9_ram_getxer(struct thread *thread, uint64_t *value)
 {
-	CHECK_ERR(thread_getspr(thread, 1, value));
+	CHECK_ERR(thread_getspr(&thread->target, 1, value));
 
 	return 0;
 }
 
-static int p9_ram_putxer(struct pdbg_target *thread, uint64_t value)
+static int p9_ram_putxer(struct thread *thread, uint64_t value)
 {
-	CHECK_ERR(thread_putspr(thread, 1, value));
+	CHECK_ERR(thread_putspr(&thread->target, 1, value));
 
 	return 0;
 
diff --git a/src/pdbgproxy.c b/src/pdbgproxy.c
index ee99cfb..bb7c7b3 100644
--- a/src/pdbgproxy.c
+++ b/src/pdbgproxy.c
@@ -285,7 +285,7 @@ static void put_mem(uint64_t *stack, void *priv)
 		data = attn_opcode;
 
 		/* Need to enable the attn instruction in HID0 */
-		if (thread->enable_attn(thread_target))
+		if (thread->enable_attn(thread))
 			goto out;
 	} else
 		stack[2] = __builtin_bswap64(stack[2]) >> 32;
-- 
2.26.2



More information about the Pdbg mailing list