Skip to contentSkip to conversation

parisc: Fix locking in pdc_iodc_print() firmware call

PATCH New Helge Deller 1 participant 4 messages Nov 26, 2022 PA-RISC architecture

lore
Share & tools
lore

Helge Deller

author maintainer
#0 lore raw

PATCH 1/4 parisc: Fix locking in pdc_iodc_print() firmware call

Utilize pdc_lock spinlock to protect parallel modifications of the iodc_dbuf[] buffer, check length to prevent buffer overflow of iodc_dbuf[], drop the iodc_retbuf[] buffer and fix some wrong indentings.

---
 arch/parisc/kernel/firmware.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
arch/parisc/kernel/firmware.c +12 -11
index 6a7e315bcc2e..c19506fc4e4e 100644
@@ -1288,9 +1288,8 @@ void pdc_io_reset_devices(void)
 
 #endif /* defined(BOOTLOADER) */
 
-/* locked by pdc_console_lock */
-static int __attribute__((aligned(8)))   iodc_retbuf[32];
-static char __attribute__((aligned(64))) iodc_dbuf[4096];
+/* locked by pdc_lock */
+char iodc_dbuf[2*4096] __page_aligned_bss;
 
 /**
  * pdc_iodc_print - Console print using IODC.
@@ -1307,6 +1306,9 @@ int pdc_iodc_print(const unsigned char *str, unsigned count)
 	unsigned int i;
 	unsigned long flags;
 
+	count = min(count, sizeof(iodc_dbuf));
+
+	spin_lock_irqsave(&pdc_lock, flags);
 	for (i = 0; i < count;) {
 		switch(str[i]) {
 		case '\n':
@@ -1322,12 +1324,11 @@ int pdc_iodc_print(const unsigned char *str, unsigned count)
 	}
 
 print:
-        spin_lock_irqsave(&pdc_lock, flags);
-        real32_call(PAGE0->mem_cons.iodc_io,
-                    (unsigned long)PAGE0->mem_cons.hpa, ENTRY_IO_COUT,
-                    PAGE0->mem_cons.spa, __pa(PAGE0->mem_cons.dp.layers),
-                    __pa(iodc_retbuf), 0, __pa(iodc_dbuf), i, 0);
-        spin_unlock_irqrestore(&pdc_lock, flags);
+	real32_call(PAGE0->mem_cons.iodc_io,
+		(unsigned long)PAGE0->mem_cons.hpa, ENTRY_IO_COUT,
+		PAGE0->mem_cons.spa, __pa(PAGE0->mem_cons.dp.layers),
+		__pa(pdc_result), 0, __pa(iodc_dbuf), i, 0);
+	spin_unlock_irqrestore(&pdc_lock, flags);
 
 	return i;
 }
@@ -1354,10 +1355,10 @@ int pdc_iodc_getc(void)
 	real32_call(PAGE0->mem_kbd.iodc_io,
 		    (unsigned long)PAGE0->mem_kbd.hpa, ENTRY_IO_CIN,
 		    PAGE0->mem_kbd.spa, __pa(PAGE0->mem_kbd.dp.layers),
-		    __pa(iodc_retbuf), 0, __pa(iodc_dbuf), 1, 0);
+		    __pa(pdc_result), 0, __pa(iodc_dbuf), 1, 0);
 
 	ch = *iodc_dbuf;
-	status = *iodc_retbuf;
+	status = *pdc_result;
 	spin_unlock_irqrestore(&pdc_lock, flags);
 
 	if (status == 0)

--
2.38.1

author maintainer
#1 lore raw

PATCH 2/4 parisc: Merge hpmc_iodc_buf and iodc_dbuf buffers

The HPMC handler can share the IODC buffer with the various firmware calls.

---
 arch/parisc/kernel/hpmc.S | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
arch/parisc/kernel/hpmc.S +2 -6
index eb2e4bd67035..1dc85e963a17 100644
@@ -44,15 +44,11 @@
 	 */
 
 	.import toc_stack,data
+	.import iodc_dbuf,data
 #define hpmc_stack	toc_stack	/* re-use the TOC stack */
-
+#define hpmc_iodc_buf	iodc_dbuf	/* re-use IODC buffer from firmware.c */
 #define HPMC_IODC_BUF_SIZE 0x8000
 
-	__PAGE_ALIGNED_BSS
-	.align 4096
-hpmc_iodc_buf:
-	.block HPMC_IODC_BUF_SIZE
-
 	.section .bss
 	.align 8
 hpmc_raddr:

--
2.38.1

author maintainer
#2 lore raw

PATCH 3/4 parisc: Drop duplicate kgdb_pdc console in pdc_cons.c

In reply to Helge Deller

The kgdb console is already registered in kgdb.c, so the duplicate code can be dropped.

---
 arch/parisc/kernel/pdc_cons.c | 36 -----------------------------------
 1 file changed, 36 deletions(-)
arch/parisc/kernel/pdc_cons.c +0 -36
index 7d0989f523d0..eebfc3e3d94c 100644
@@ -8,50 +8,18 @@
 #include <linux/console.h>
 #include <linux/init.h>
 #include <linux/serial_core.h>
-#include <linux/kgdb.h>
 #include <asm/page.h>		/* for PAGE0 */
 #include <asm/pdc.h>		/* for iodc_call() proto and friends */
 
-static DEFINE_SPINLOCK(pdc_console_lock);
-
 static void pdc_console_write(struct console *co, const char *s, unsigned count)
 {
 	int i = 0;
-	unsigned long flags;
 
-	spin_lock_irqsave(&pdc_console_lock, flags);
 	do {
 		i += pdc_iodc_print(s + i, count - i);
 	} while (i < count);
-	spin_unlock_irqrestore(&pdc_console_lock, flags);
-}
-
-#ifdef CONFIG_KGDB
-static int kgdb_pdc_read_char(void)
-{
-	int c;
-	unsigned long flags;
-
-	spin_lock_irqsave(&pdc_console_lock, flags);
-	c = pdc_iodc_getc();
-	spin_unlock_irqrestore(&pdc_console_lock, flags);
-
-	return (c <= 0) ? NO_POLL_CHAR : c;
 }
 
-static void kgdb_pdc_write_char(u8 chr)
-{
-	if (PAGE0->mem_cons.cl_class != CL_DUPLEX)
-		pdc_console_write(NULL, &chr, 1);
-}
-
-static struct kgdb_io kgdb_pdc_io_ops = {
-	.name = "kgdb_pdc",
-	.read_char = kgdb_pdc_read_char,
-	.write_char = kgdb_pdc_write_char,
-};
-#endif
-
 static int __init pdc_earlycon_setup(struct earlycon_device *device,
 				     const char *opt)
 {
@@ -65,10 +33,6 @@ static int __init pdc_earlycon_setup(struct earlycon_device *device,
 	earlycon_console->write = pdc_console_write;
 	device->port.iotype = UPIO_MEM32BE;
 
-#ifdef CONFIG_KGDB
-	kgdb_register_io_module(&kgdb_pdc_io_ops);
-#endif
-
 	return 0;
 }
 

--
2.38.1

author maintainer
#3 lore raw

PATCH 4/4 parisc: Fix kgdb_pdc console read/write funtions

In reply to Helge Deller

Allow the kgdb debugger to read chars from PDC firmware console (keyboard or serial port), and fix output to only show up on monitors since the output to serial ports is already happens in the kgdb serial driver.

---
 arch/parisc/kernel/kgdb.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
arch/parisc/kernel/kgdb.c +11 -2
index ab7620f695be..586046d52712 100644
@@ -13,6 +13,7 @@
 #include <linux/notifier.h>
 #include <linux/kdebug.h>
 #include <linux/uaccess.h>
+#include <linux/serial_core.h>
 #include <asm/ptrace.h>
 #include <asm/traps.h>
 #include <asm/processor.h>
@@ -211,14 +212,22 @@ int kgdb_arch_handle_exception(int trap, int signo,
 
 /* KGDB console driver which uses PDC to read chars from keyboard */
 
+static int kgdb_pdc_read_char(void)
+{
+        int c = pdc_iodc_getc();
+
+        return (c <= 0) ? NO_POLL_CHAR : c;
+}
+
 static void kgdb_pdc_write_char(u8 chr)
 {
-	/* no need to print char. kgdb will do it. */
+	if (PAGE0->mem_cons.cl_class != CL_DUPLEX)
+		pdc_iodc_print(&chr, 1);
 }
 
 static struct kgdb_io kgdb_pdc_io_ops = {
 	.name		= "kgdb_pdc",
-	.read_char	= pdc_iodc_getc,
+	.read_char	= kgdb_pdc_read_char,
 	.write_char	= kgdb_pdc_write_char,
 };
 

--
2.38.1