173 lines · plain
1// SPDX-License-Identifier: GPL-2.0-only2// Copyright (C) 2015-2021 ARM Limited.3// Original author: Dave Martin <Dave.Martin@arm.com>4//5// Utility functions for assembly code.6 7#include <asm/unistd.h>8#include "assembler.h"9 10// Print a single character x0 to stdout11// Clobbers x0-x2,x812function putc13 str x0, [sp, #-16]!14 15 mov x0, #1 // STDOUT_FILENO16 mov x1, sp17 mov x2, #118 mov x8, #__NR_write19 svc #020 21 add sp, sp, #1622 ret23endfunction24.globl putc25 26// Print a NUL-terminated string starting at address x0 to stdout27// Clobbers x0-x3,x828function puts29 mov x1, x030 31 mov x2, #0320: ldrb w3, [x0], #133 cbz w3, 1f34 add x2, x2, #135 b 0b36 371: mov w0, #1 // STDOUT_FILENO38 mov x8, #__NR_write39 svc #040 41 ret42endfunction43.globl puts44 45// Print an unsigned decimal number x0 to stdout46// Clobbers x0-x4,x847function putdec48 mov x1, sp49 str x30, [sp, #-32]! // Result can't be > 20 digits50 51 mov x2, #052 strb w2, [x1, #-1]! // Write the NUL terminator53 54 mov x2, #10550: udiv x3, x0, x2 // div-mod loop to generate the digits56 msub x0, x3, x2, x057 add w0, w0, #'0'58 strb w0, [x1, #-1]!59 mov x0, x360 cbnz x3, 0b61 62 ldrb w0, [x1]63 cbnz w0, 1f64 mov w0, #'0' // Print "0" for 0, not ""65 strb w0, [x1, #-1]!66 671: mov x0, x168 bl puts69 70 ldr x30, [sp], #3271 ret72endfunction73.globl putdec74 75// Print an unsigned decimal number x0 to stdout, followed by a newline76// Clobbers x0-x5,x877function putdecn78 mov x5, x3079 80 bl putdec81 mov x0, #'\n'82 bl putc83 84 ret x585endfunction86.globl putdecn87 88// Clobbers x0-x3,x889function puthexb90 str x30, [sp, #-0x10]!91 92 mov w3, w093 lsr w0, w0, #494 bl puthexnibble95 mov w0, w396 97 ldr x30, [sp], #0x1098 // fall through to puthexnibble99endfunction100.globl puthexb101 102// Clobbers x0-x2,x8103function puthexnibble104 and w0, w0, #0xf105 cmp w0, #10106 blo 1f107 add w0, w0, #'a' - ('9' + 1)1081: add w0, w0, #'0'109 b putc110endfunction111.globl puthexnibble112 113// x0=data in, x1=size in, clobbers x0-x5,x8114function dumphex115 str x30, [sp, #-0x10]!116 117 mov x4, x0118 mov x5, x1119 1200: subs x5, x5, #1121 b.lo 1f122 ldrb w0, [x4], #1123 bl puthexb124 b 0b125 1261: ldr x30, [sp], #0x10127 ret128endfunction129.globl dumphex130 131 // Trivial memory copy: copy x2 bytes, starting at address x1, to address x0.132// Clobbers x0-x3133function memcpy134 cmp x2, #0135 b.eq 1f1360: ldrb w3, [x1], #1137 strb w3, [x0], #1138 subs x2, x2, #1139 b.ne 0b1401: ret141endfunction142.globl memcpy143 144// Fill x1 bytes starting at x0 with 0xae (for canary purposes)145// Clobbers x1, x2.146function memfill_ae147 mov w2, #0xae148 b memfill149endfunction150.globl memfill_ae151 152// Fill x1 bytes starting at x0 with 0.153// Clobbers x1, x2.154function memclr155 mov w2, #0156endfunction157.globl memclr158 // fall through to memfill159 160// Trivial memory fill: fill x1 bytes starting at address x0 with byte w2161// Clobbers x1162function memfill163 cmp x1, #0164 b.eq 1f165 1660: strb w2, [x0], #1167 subs x1, x1, #1168 b.ne 0b169 1701: ret171endfunction172.globl memfill173