# Why no output from printf in user mode?

**URL:** <https://forums.sifive.com/t/why-no-output-from-printf-in-user-mode/2534>\
**Category:** HiFive1 Rev B\
**Created:** [June 29, 2019, 3:40pm UTC](https://forums.sifive.com/t/why-no-output-from-printf-in-user-mode/2534 "2019-06-29T15:40:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bengtb](https://avatars.discourse-cdn.com/v4/letter/b/bbce88/32.png) [@bengtb](https://forums.sifive.com/u/bengtb)\
**Post date:** [June 29, 2019, 3:40pm UTC](https://forums.sifive.com/t/why-no-output-from-printf-in-user-mode/2534/1 "2019-06-29T15:40:31Z")

</div>

I have a HiFive rev b and I’m trying out different things out in assembly. I’m using the command line environment riscv64-unknown-elf-gcc-8.2.0-2019.02.0-x86\_64-apple-darwin and the latest version of freedom-e-sdk.

In machine mode I have no problem what so ever to call printf from assembly, but I get no output when calling printf from user mode, nor do I get any output when calling printf in the isr after causing a fault in user mode. What could be the reason?

I have tested several things to find the issue:

- I can call printf successfully from machine mode
- I can read, write and execute in user mode
- I have expanded the stack space
- I have called other methods written in C and there is not problem calling a method such as “int addition(int a, int b)”. Adding printf gives no output.
- if I induce a fault in machine mode then a call to printf in the isr works
- if I induce a fault in user mode then a call to printf gives no printout
- my own methods using the uart works in user mode
- I have checked that the registers such as gp etc have to same values as in machine mode

Here is my C code that calls the assembly:

> Blockquote  
> #include \<stdio.h\>  
> extern void assembler();
> 
> int main() {  
> assembler();
> 
> return 0;  
> }

Here is my assembly ( I don’t know what to do to prevent comments to be interpreted as titles):

> .global assembler  
> .section .text
> 
> .equ pmp\_A, 8 # 0b01 \<\< 3, TOR  
> .equ pmp\_R, 1 # 1, read  
> .equ pmp\_W, 2 # 2, write  
> .equ pmp\_X, 4 # 4, execute  
> .equ PMP\_CONFIG\_0, (pmp\_A | pmp\_X | pmp\_W | pmp\_R)
> 
> .equ mstatus\_mprv, 0x20000 # 1 \<\< 17, mprv = modify privilege  
> .equ mstatus\_mpp, 0x00000 # 0b00 \<\< 11, mpp = privilege mode = user mode  
> .equ MSTATUS\_MODPRIV, (mstatus\_mprv | mstatus\_mpp)
> 
> .equ mstatus\_mpp\_mask, 0x01800 # 3 \<\< 11  
> .equ mstatus\_mpp\_user, 0 # 0b00 = usermode
> 
> assembler:  
> la a0, mhello  
> call printf
> 
> # establish the interrupt service route
> 
> la t0, fault  
> csrw mtvec, t0
> 
> # every address, below max
> 
> li t0, -1  
> srli t0, t0, 2  
> csrw pmpaddr0, t0
> 
> # from TOR and below, read, write and execute in user mode
> 
> li t0, PMP\_CONFIG\_0  
> csrw pmpcfg0, t0
> 
> # check protection for user mode according to pmp configuration
> 
> csrr t1, mstatus  
> li t0, MSTATUS\_MODPRIV  
> or t0, t0, t1  
> csrw mstatus, t0
> 
> # 
> 
> # setup user mode registers
> 
> # 
> 
> csrr t0, mstatus  
> li t1, mstatus\_mpp\_mask  
> not t1, t1  
> and t0, t0, t1  
> li t1, mstatus\_mpp\_user  
> or t0, t0, t1  
> csrw mstatus, t0 # indicate that we want user mode
> 
> la sp, ustack  
> la t0, usermode  
> csrw mepc, t0  
> la ra, usermode  
> mret # “return” to previous mode (carefully set up to be user mode)
> 
> la a0, wtf  
> call print\_string
> 
> ret
> 
> usermode:  
> la a0, hello  
> call printf
> 
> 100:  
> j 100b
> 
> fault:
> 
> # 
> 
> # This fails if something happens in user mode…
> 
> # 
> 
> csrr a2, mstatus  
> csrr a1, mcause  
> la a0, error  
> call printf
> 
> 100:  
> j 100b
> 
> hello:  
> .string “Hello from user mode.\r\n”  
> mhello:  
> .string “Hello from machine mode.\r\n”  
> wtf:  
> .string “\033[91mWTF?\r\n”  
> error:  
> .string “\033[91mInterrupted, cause: 0x%x, mstatus: 0x%x\r\n”
> 
> .section .data  
> .balign 16
> 
> test:  
> .word 0xdeadbeef
> 
> guard:  
> .word 0xdeadbeef # if the stack has been overrun, then this guard will not say 0xdeadbeef anymore
> 
> .space 24\*4  
> ustack:

---

<div class="post-metadata">

**Author:** ![bruce](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/bruce/32/452_2.png) [@bruce](https://forums.sifive.com/u/bruce)\
**Post date:** [June 30, 2019, 12:36am UTC](https://forums.sifive.com/t/why-no-output-from-printf-in-user-mode/2534/2 "2019-06-30T00:36:45Z")

</div>

User mode is user mode – it has no privileges. If you want to do something like I/O from User mode then it is generally your responsibility to ensure that you have installed some kind of system call service in machine mode that User mode can use, typically by installing a handler for the ECALL instruction and defining some kind of SBI.

---

<div class="post-metadata">

**Author:** ![bruce](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/bruce/32/452_2.png) [@bruce](https://forums.sifive.com/u/bruce)\
**Post date:** [June 30, 2019, 12:40am UTC](https://forums.sifive.com/t/why-no-output-from-printf-in-user-mode/2534/3 "2019-06-30T00:40:30Z")

</div>

> [@bengtb](#):
>
> la ra, usermode

I’m not sure of the point of this. It’s not going to do anything useful for the mret, and will only result in the ret after printing wtf (if that somehow happens, which it shouldn’t) jumping to print “Hello from user mode”. In machine mode.

---

<div class="post-metadata">

**Author:** ![bengtb](https://avatars.discourse-cdn.com/v4/letter/b/bbce88/32.png) [@bengtb](https://forums.sifive.com/u/bengtb)\
**Post date:** [June 30, 2019, 9:57am UTC](https://forums.sifive.com/t/why-no-output-from-printf-in-user-mode/2534/4 "2019-06-30T09:57:53Z")

</div>

Than you for your answer.  
My goal is to use ecall to create a set of system services. This is me getting closer.

**What do you mean with privileges in this case? What is it that user mode is lacking?** It can access all the memory, including the uart. When I swap printf with my own code that prints using the uart it works great in user mode.

---

<div class="post-metadata">

**Author:** ![bengtb](https://avatars.discourse-cdn.com/v4/letter/b/bbce88/32.png) [@bengtb](https://forums.sifive.com/u/bengtb)\
**Post date:** [June 30, 2019, 10:01am UTC](https://forums.sifive.com/t/why-no-output-from-printf-in-user-mode/2534/5 "2019-06-30T10:01:05Z")

</div>

> [@bruce](#):
>
> > [@bengtb](#):
> >
> > la ra, usermode
> 
> I’m not sure of the point of this.

Thank you for taking time to read my code. I should have removed this instruction. It’s a left over from me experimenting and trying to find the answer to why printf does not print anything. As you say, it doesn’t add anything.
