diff mbox

[kvm-unit-tests,08/14] powerpc/ppc64: add HV putchar

Message ID 1438612891-3718-9-git-send-email-drjones@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Andrew Jones Aug. 3, 2015, 2:41 p.m. UTC
Add the hvcall for putchar and use it in puts. That, along with a
couple more lines in start to prepare for C code, and a branch to
main(), gets us "hello world". Run with

qemu-system-ppc64 -M pseries            \
        -bios powerpc/boot_rom.bin      \
        -display none -serial stdio     \
        -kernel powerpc/selftest.elf

(We're still not relocating yet, that comes in a later patch. Thus,
 testing hello-world at this point requires a hacked QEMU and linking
 the unit test at QEMU's kernel load address.)

Signed-off-by: Andrew Jones <drjones@redhat.com>
---
 lib/powerpc/io.c        | 15 +++++++++++++--
 powerpc/Makefile.common |  1 +
 powerpc/cstart64.S      | 27 +++++++++++++++++++++++++++
 3 files changed, 41 insertions(+), 2 deletions(-)

Comments

David Gibson Aug. 4, 2015, 3:50 a.m. UTC | #1
On Mon, Aug 03, 2015 at 04:41:25PM +0200, Andrew Jones wrote:
> Add the hvcall for putchar and use it in puts. That, along with a
> couple more lines in start to prepare for C code, and a branch to
> main(), gets us "hello world". Run with
> 
> qemu-system-ppc64 -M pseries            \
>         -bios powerpc/boot_rom.bin      \
>         -display none -serial stdio     \
>         -kernel powerpc/selftest.elf
> 
> (We're still not relocating yet, that comes in a later patch. Thus,
>  testing hello-world at this point requires a hacked QEMU and linking
>  the unit test at QEMU's kernel load address.)
> 
> Signed-off-by: Andrew Jones <drjones@redhat.com>
> ---
>  lib/powerpc/io.c        | 15 +++++++++++++--
>  powerpc/Makefile.common |  1 +
>  powerpc/cstart64.S      | 27 +++++++++++++++++++++++++++
>  3 files changed, 41 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/powerpc/io.c b/lib/powerpc/io.c
> index cf3b6347e1e46..a7eaafeca9205 100644
> --- a/lib/powerpc/io.c
> +++ b/lib/powerpc/io.c
> @@ -6,15 +6,26 @@
>   * This work is licensed under the terms of the GNU LGPL, version 2.
>   */
>  #include <libcflat.h>
> +#include <asm/spinlock.h>
> +
> +extern void halt(int code);
> +extern void putchar(int c);
> +
> +static struct spinlock uart_lock;
>  
>  void io_init(void)
>  {
>  }
>  
> -void puts(const char *s __unused)
> +void puts(const char *s)
>  {
> +	spin_lock(&uart_lock);
> +	while (*s)
> +		putchar(*s++);
> +	spin_unlock(&uart_lock);
>  }
>  
> -void exit(int code __unused)
> +void exit(int code)
>  {
> +	halt(code);
>  }
> diff --git a/powerpc/Makefile.common b/powerpc/Makefile.common
> index 5cd3ea8085038..d6356540918a5 100644
> --- a/powerpc/Makefile.common
> +++ b/powerpc/Makefile.common
> @@ -26,6 +26,7 @@ CFLAGS += -ffreestanding
>  CFLAGS += -Wextra
>  CFLAGS += -O2
>  CFLAGS += -I lib -I lib/libfdt
> +CFLAGS += -Wa,-mregnames
>  
>  asm-offsets = lib/$(ARCH)/asm-offsets.h
>  include scripts/asm-offsets.mak
> diff --git a/powerpc/cstart64.S b/powerpc/cstart64.S
> index d9b77f44f9e0e..d7c51cd352ee4 100644
> --- a/powerpc/cstart64.S
> +++ b/powerpc/cstart64.S
> @@ -6,14 +6,41 @@
>   * This work is licensed under the terms of the GNU LGPL, version 2.
>   */
>  
> +#define HVSC			.long 0x44000022

Unless your assembler is really old, you could probably use "sc 1"
inline, instead of this macro.  Not that it really matters.

> +#define H_PUT_TERM_CHAR		0x58
> +
> +#define LOAD_REG_IMMEDIATE(reg,expr)		\
> +	lis	reg,(expr)@highest;		\
> +	ori	reg,reg,(expr)@higher;		\
> +	rldicr	reg,reg,32,31;			\
> +	oris	reg,reg,(expr)@h;		\
> +	ori	reg,reg,(expr)@l;
> +
> +#define LOAD_REG_ADDR(reg,name)			\
> +	ld	reg,name@got(r2)
> +
>  .section .init
>  
>  .globl start
>  start:
> +	LOAD_REG_IMMEDIATE(r1, stackptr)
> +	LOAD_REG_IMMEDIATE(r2, tocptr)
> +	bl	.main
> +	bl	.exit

Is this built for ppc64 or ppc64le?  IIUC under the new ABI version
usually used on ppc64le, function descriptors are no longer used.  And
even under the old ABI, I think the assembler now has the smarts to
avoid explicitly referencing the .-symbols.

>  	b	halt
>  
>  .text
> +.align 3
>  
>  .globl halt
>  halt:
>  1:	b	1b
> +
> +.globl putchar
> +putchar:
> +	sldi	r6, r3, 56
> +	li	r3, H_PUT_TERM_CHAR
> +	li	r4, 0	/* vty-reg 0 means to use the default vty */
> +	li	r5, 1	/* sending just 1 byte */
> +	HVSC
> +	blr
Andrew Jones Aug. 4, 2015, 7:33 a.m. UTC | #2
On Tue, Aug 04, 2015 at 01:50:39PM +1000, David Gibson wrote:
> On Mon, Aug 03, 2015 at 04:41:25PM +0200, Andrew Jones wrote:
> > Add the hvcall for putchar and use it in puts. That, along with a
> > couple more lines in start to prepare for C code, and a branch to
> > main(), gets us "hello world". Run with
> > 
> > qemu-system-ppc64 -M pseries            \
> >         -bios powerpc/boot_rom.bin      \
> >         -display none -serial stdio     \
> >         -kernel powerpc/selftest.elf
> > 
> > (We're still not relocating yet, that comes in a later patch. Thus,
> >  testing hello-world at this point requires a hacked QEMU and linking
> >  the unit test at QEMU's kernel load address.)
> > 
> > Signed-off-by: Andrew Jones <drjones@redhat.com>
> > ---
> >  lib/powerpc/io.c        | 15 +++++++++++++--
> >  powerpc/Makefile.common |  1 +
> >  powerpc/cstart64.S      | 27 +++++++++++++++++++++++++++
> >  3 files changed, 41 insertions(+), 2 deletions(-)
> > 
> > diff --git a/lib/powerpc/io.c b/lib/powerpc/io.c
> > index cf3b6347e1e46..a7eaafeca9205 100644
> > --- a/lib/powerpc/io.c
> > +++ b/lib/powerpc/io.c
> > @@ -6,15 +6,26 @@
> >   * This work is licensed under the terms of the GNU LGPL, version 2.
> >   */
> >  #include <libcflat.h>
> > +#include <asm/spinlock.h>
> > +
> > +extern void halt(int code);
> > +extern void putchar(int c);
> > +
> > +static struct spinlock uart_lock;
> >  
> >  void io_init(void)
> >  {
> >  }
> >  
> > -void puts(const char *s __unused)
> > +void puts(const char *s)
> >  {
> > +	spin_lock(&uart_lock);
> > +	while (*s)
> > +		putchar(*s++);
> > +	spin_unlock(&uart_lock);
> >  }
> >  
> > -void exit(int code __unused)
> > +void exit(int code)
> >  {
> > +	halt(code);
> >  }
> > diff --git a/powerpc/Makefile.common b/powerpc/Makefile.common
> > index 5cd3ea8085038..d6356540918a5 100644
> > --- a/powerpc/Makefile.common
> > +++ b/powerpc/Makefile.common
> > @@ -26,6 +26,7 @@ CFLAGS += -ffreestanding
> >  CFLAGS += -Wextra
> >  CFLAGS += -O2
> >  CFLAGS += -I lib -I lib/libfdt
> > +CFLAGS += -Wa,-mregnames
> >  
> >  asm-offsets = lib/$(ARCH)/asm-offsets.h
> >  include scripts/asm-offsets.mak
> > diff --git a/powerpc/cstart64.S b/powerpc/cstart64.S
> > index d9b77f44f9e0e..d7c51cd352ee4 100644
> > --- a/powerpc/cstart64.S
> > +++ b/powerpc/cstart64.S
> > @@ -6,14 +6,41 @@
> >   * This work is licensed under the terms of the GNU LGPL, version 2.
> >   */
> >  
> > +#define HVSC			.long 0x44000022
> 
> Unless your assembler is really old, you could probably use "sc 1"
> inline, instead of this macro.  Not that it really matters.

It makes sense to switch to sc for a new project. I'll do that.

> 
> > +#define H_PUT_TERM_CHAR		0x58
> > +
> > +#define LOAD_REG_IMMEDIATE(reg,expr)		\
> > +	lis	reg,(expr)@highest;		\
> > +	ori	reg,reg,(expr)@higher;		\
> > +	rldicr	reg,reg,32,31;			\
> > +	oris	reg,reg,(expr)@h;		\
> > +	ori	reg,reg,(expr)@l;
> > +
> > +#define LOAD_REG_ADDR(reg,name)			\
> > +	ld	reg,name@got(r2)
> > +
> >  .section .init
> >  
> >  .globl start
> >  start:
> > +	LOAD_REG_IMMEDIATE(r1, stackptr)
> > +	LOAD_REG_IMMEDIATE(r2, tocptr)
> > +	bl	.main
> > +	bl	.exit
> 
> Is this built for ppc64 or ppc64le?  IIUC under the new ABI version
> usually used on ppc64le, function descriptors are no longer used.  And
> even under the old ABI, I think the assembler now has the smarts to
> avoid explicitly referencing the .-symbols.

For now only building for ppc64, but eventually both. I'll remove the
dots.

Thanks,
drew

> 
> >  	b	halt
> >  
> >  .text
> > +.align 3
> >  
> >  .globl halt
> >  halt:
> >  1:	b	1b
> > +
> > +.globl putchar
> > +putchar:
> > +	sldi	r6, r3, 56
> > +	li	r3, H_PUT_TERM_CHAR
> > +	li	r4, 0	/* vty-reg 0 means to use the default vty */
> > +	li	r5, 1	/* sending just 1 byte */
> > +	HVSC
> > +	blr
> 
> -- 
> David Gibson			| I'll have my music baroque, and my code
> david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
> 				| _way_ _around_!
> http://www.ozlabs.org/~dgibson


--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/lib/powerpc/io.c b/lib/powerpc/io.c
index cf3b6347e1e46..a7eaafeca9205 100644
--- a/lib/powerpc/io.c
+++ b/lib/powerpc/io.c
@@ -6,15 +6,26 @@ 
  * This work is licensed under the terms of the GNU LGPL, version 2.
  */
 #include <libcflat.h>
+#include <asm/spinlock.h>
+
+extern void halt(int code);
+extern void putchar(int c);
+
+static struct spinlock uart_lock;
 
 void io_init(void)
 {
 }
 
-void puts(const char *s __unused)
+void puts(const char *s)
 {
+	spin_lock(&uart_lock);
+	while (*s)
+		putchar(*s++);
+	spin_unlock(&uart_lock);
 }
 
-void exit(int code __unused)
+void exit(int code)
 {
+	halt(code);
 }
diff --git a/powerpc/Makefile.common b/powerpc/Makefile.common
index 5cd3ea8085038..d6356540918a5 100644
--- a/powerpc/Makefile.common
+++ b/powerpc/Makefile.common
@@ -26,6 +26,7 @@  CFLAGS += -ffreestanding
 CFLAGS += -Wextra
 CFLAGS += -O2
 CFLAGS += -I lib -I lib/libfdt
+CFLAGS += -Wa,-mregnames
 
 asm-offsets = lib/$(ARCH)/asm-offsets.h
 include scripts/asm-offsets.mak
diff --git a/powerpc/cstart64.S b/powerpc/cstart64.S
index d9b77f44f9e0e..d7c51cd352ee4 100644
--- a/powerpc/cstart64.S
+++ b/powerpc/cstart64.S
@@ -6,14 +6,41 @@ 
  * This work is licensed under the terms of the GNU LGPL, version 2.
  */
 
+#define HVSC			.long 0x44000022
+#define H_PUT_TERM_CHAR		0x58
+
+#define LOAD_REG_IMMEDIATE(reg,expr)		\
+	lis	reg,(expr)@highest;		\
+	ori	reg,reg,(expr)@higher;		\
+	rldicr	reg,reg,32,31;			\
+	oris	reg,reg,(expr)@h;		\
+	ori	reg,reg,(expr)@l;
+
+#define LOAD_REG_ADDR(reg,name)			\
+	ld	reg,name@got(r2)
+
 .section .init
 
 .globl start
 start:
+	LOAD_REG_IMMEDIATE(r1, stackptr)
+	LOAD_REG_IMMEDIATE(r2, tocptr)
+	bl	.main
+	bl	.exit
 	b	halt
 
 .text
+.align 3
 
 .globl halt
 halt:
 1:	b	1b
+
+.globl putchar
+putchar:
+	sldi	r6, r3, 56
+	li	r3, H_PUT_TERM_CHAR
+	li	r4, 0	/* vty-reg 0 means to use the default vty */
+	li	r5, 1	/* sending just 1 byte */
+	HVSC
+	blr