xref: /linux/arch/powerpc/boot/string.S (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*2874c5fdSThomas Gleixner/* SPDX-License-Identifier: GPL-2.0-or-later */
294b212c2SPaul Mackerras/*
394b212c2SPaul Mackerras * Copyright (C) Paul Mackerras 1997.
494b212c2SPaul Mackerras *
594b212c2SPaul Mackerras * NOTE: this code runs in 32 bit mode and is packaged as ELF32.
694b212c2SPaul Mackerras */
794b212c2SPaul Mackerras
894b212c2SPaul Mackerras#include "ppc_asm.h"
994b212c2SPaul Mackerras
1094b212c2SPaul Mackerras	.text
1194b212c2SPaul Mackerras	.globl	strcpy
1294b212c2SPaul Mackerrasstrcpy:
1394b212c2SPaul Mackerras	addi	r5,r3,-1
1494b212c2SPaul Mackerras	addi	r4,r4,-1
1594b212c2SPaul Mackerras1:	lbzu	r0,1(r4)
1694b212c2SPaul Mackerras	cmpwi	0,r0,0
1794b212c2SPaul Mackerras	stbu	r0,1(r5)
1894b212c2SPaul Mackerras	bne	1b
1994b212c2SPaul Mackerras	blr
2094b212c2SPaul Mackerras
2194b212c2SPaul Mackerras	.globl	strncpy
2294b212c2SPaul Mackerrasstrncpy:
2394b212c2SPaul Mackerras	cmpwi	0,r5,0
2494b212c2SPaul Mackerras	beqlr
2594b212c2SPaul Mackerras	mtctr	r5
2694b212c2SPaul Mackerras	addi	r6,r3,-1
2794b212c2SPaul Mackerras	addi	r4,r4,-1
2894b212c2SPaul Mackerras1:	lbzu	r0,1(r4)
2994b212c2SPaul Mackerras	cmpwi	0,r0,0
3094b212c2SPaul Mackerras	stbu	r0,1(r6)
3194b212c2SPaul Mackerras	bdnzf	2,1b		/* dec ctr, branch if ctr != 0 && !cr0.eq */
3294b212c2SPaul Mackerras	blr
3394b212c2SPaul Mackerras
3494b212c2SPaul Mackerras	.globl	strcat
3594b212c2SPaul Mackerrasstrcat:
3694b212c2SPaul Mackerras	addi	r5,r3,-1
3794b212c2SPaul Mackerras	addi	r4,r4,-1
3894b212c2SPaul Mackerras1:	lbzu	r0,1(r5)
3994b212c2SPaul Mackerras	cmpwi	0,r0,0
4094b212c2SPaul Mackerras	bne	1b
4194b212c2SPaul Mackerras	addi	r5,r5,-1
4294b212c2SPaul Mackerras1:	lbzu	r0,1(r4)
4394b212c2SPaul Mackerras	cmpwi	0,r0,0
4494b212c2SPaul Mackerras	stbu	r0,1(r5)
4594b212c2SPaul Mackerras	bne	1b
4694b212c2SPaul Mackerras	blr
4794b212c2SPaul Mackerras
480ae0b545SDavid Gibson	.globl	strchr
490ae0b545SDavid Gibsonstrchr:
500ae0b545SDavid Gibson	addi	r3,r3,-1
510ae0b545SDavid Gibson1:	lbzu	r0,1(r3)
520ae0b545SDavid Gibson	cmpw	0,r0,r4
530ae0b545SDavid Gibson	beqlr
540ae0b545SDavid Gibson	cmpwi	0,r0,0
550ae0b545SDavid Gibson	bne	1b
560ae0b545SDavid Gibson	li	r3,0
570ae0b545SDavid Gibson	blr
580ae0b545SDavid Gibson
5994b212c2SPaul Mackerras	.globl	strcmp
6094b212c2SPaul Mackerrasstrcmp:
6194b212c2SPaul Mackerras	addi	r5,r3,-1
6294b212c2SPaul Mackerras	addi	r4,r4,-1
6394b212c2SPaul Mackerras1:	lbzu	r3,1(r5)
6494b212c2SPaul Mackerras	cmpwi	1,r3,0
6594b212c2SPaul Mackerras	lbzu	r0,1(r4)
6694b212c2SPaul Mackerras	subf.	r3,r0,r3
6794b212c2SPaul Mackerras	beqlr	1
6894b212c2SPaul Mackerras	beq	1b
6994b212c2SPaul Mackerras	blr
7094b212c2SPaul Mackerras
710ae0b545SDavid Gibson	.globl	strncmp
720ae0b545SDavid Gibsonstrncmp:
730ae0b545SDavid Gibson	mtctr	r5
740ae0b545SDavid Gibson	addi	r5,r3,-1
750ae0b545SDavid Gibson	addi	r4,r4,-1
760ae0b545SDavid Gibson1:	lbzu	r3,1(r5)
770ae0b545SDavid Gibson	cmpwi	1,r3,0
780ae0b545SDavid Gibson	lbzu	r0,1(r4)
790ae0b545SDavid Gibson	subf.	r3,r0,r3
800ae0b545SDavid Gibson	beqlr	1
810ae0b545SDavid Gibson	bdnzt	eq,1b
820ae0b545SDavid Gibson	blr
830ae0b545SDavid Gibson
8494b212c2SPaul Mackerras	.globl	strlen
8594b212c2SPaul Mackerrasstrlen:
8694b212c2SPaul Mackerras	addi	r4,r3,-1
8794b212c2SPaul Mackerras1:	lbzu	r0,1(r4)
8894b212c2SPaul Mackerras	cmpwi	0,r0,0
8994b212c2SPaul Mackerras	bne	1b
9094b212c2SPaul Mackerras	subf	r3,r3,r4
9194b212c2SPaul Mackerras	blr
9294b212c2SPaul Mackerras
9394b212c2SPaul Mackerras	.globl	memset
9494b212c2SPaul Mackerrasmemset:
9594b212c2SPaul Mackerras	rlwimi	r4,r4,8,16,23
9694b212c2SPaul Mackerras	rlwimi	r4,r4,16,0,15
9794b212c2SPaul Mackerras	addi	r6,r3,-4
9894b212c2SPaul Mackerras	cmplwi	0,r5,4
9994b212c2SPaul Mackerras	blt	7f
10094b212c2SPaul Mackerras	stwu	r4,4(r6)
10194b212c2SPaul Mackerras	beqlr
10294b212c2SPaul Mackerras	andi.	r0,r6,3
10394b212c2SPaul Mackerras	add	r5,r0,r5
10494b212c2SPaul Mackerras	subf	r6,r0,r6
10594b212c2SPaul Mackerras	rlwinm	r0,r5,32-2,2,31
10694b212c2SPaul Mackerras	mtctr	r0
10794b212c2SPaul Mackerras	bdz	6f
10894b212c2SPaul Mackerras1:	stwu	r4,4(r6)
10994b212c2SPaul Mackerras	bdnz	1b
11094b212c2SPaul Mackerras6:	andi.	r5,r5,3
11194b212c2SPaul Mackerras7:	cmpwi	0,r5,0
11294b212c2SPaul Mackerras	beqlr
11394b212c2SPaul Mackerras	mtctr	r5
11494b212c2SPaul Mackerras	addi	r6,r6,3
11594b212c2SPaul Mackerras8:	stbu	r4,1(r6)
11694b212c2SPaul Mackerras	bdnz	8b
11794b212c2SPaul Mackerras	blr
11894b212c2SPaul Mackerras
11994b212c2SPaul Mackerras	.globl	memmove
12094b212c2SPaul Mackerrasmemmove:
12194b212c2SPaul Mackerras	cmplw	0,r3,r4
12294b212c2SPaul Mackerras	bgt	backwards_memcpy
12394b212c2SPaul Mackerras	/* fall through */
12494b212c2SPaul Mackerras
12594b212c2SPaul Mackerras	.globl	memcpy
12694b212c2SPaul Mackerrasmemcpy:
12794b212c2SPaul Mackerras	rlwinm.	r7,r5,32-3,3,31		/* r7 = r5 >> 3 */
12894b212c2SPaul Mackerras	addi	r6,r3,-4
12994b212c2SPaul Mackerras	addi	r4,r4,-4
130b4e7de0fSPaul Mackerras	beq	3f			/* if less than 8 bytes to do */
13194b212c2SPaul Mackerras	andi.	r0,r6,3			/* get dest word aligned */
13294b212c2SPaul Mackerras	mtctr	r7
13394b212c2SPaul Mackerras	bne	5f
134b4e7de0fSPaul Mackerras	andi.	r0,r4,3			/* check src word aligned too */
135b4e7de0fSPaul Mackerras	bne	3f
13694b212c2SPaul Mackerras1:	lwz	r7,4(r4)
13794b212c2SPaul Mackerras	lwzu	r8,8(r4)
13894b212c2SPaul Mackerras	stw	r7,4(r6)
13994b212c2SPaul Mackerras	stwu	r8,8(r6)
14094b212c2SPaul Mackerras	bdnz	1b
14194b212c2SPaul Mackerras	andi.	r5,r5,7
14294b212c2SPaul Mackerras2:	cmplwi	0,r5,4
14394b212c2SPaul Mackerras	blt	3f
14494b212c2SPaul Mackerras	lwzu	r0,4(r4)
14594b212c2SPaul Mackerras	addi	r5,r5,-4
14694b212c2SPaul Mackerras	stwu	r0,4(r6)
14794b212c2SPaul Mackerras3:	cmpwi	0,r5,0
14894b212c2SPaul Mackerras	beqlr
14994b212c2SPaul Mackerras	mtctr	r5
15094b212c2SPaul Mackerras	addi	r4,r4,3
15194b212c2SPaul Mackerras	addi	r6,r6,3
15294b212c2SPaul Mackerras4:	lbzu	r0,1(r4)
15394b212c2SPaul Mackerras	stbu	r0,1(r6)
15494b212c2SPaul Mackerras	bdnz	4b
15594b212c2SPaul Mackerras	blr
15694b212c2SPaul Mackerras5:	subfic	r0,r0,4
157b4e7de0fSPaul Mackerras	cmpw	cr1,r0,r5
158b4e7de0fSPaul Mackerras	add	r7,r0,r4
159b4e7de0fSPaul Mackerras	andi.	r7,r7,3			/* will source be word-aligned too? */
160b4e7de0fSPaul Mackerras	ble	cr1,3b
161b4e7de0fSPaul Mackerras	bne	3b			/* do byte-by-byte if not */
16294b212c2SPaul Mackerras	mtctr	r0
16394b212c2SPaul Mackerras6:	lbz	r7,4(r4)
16494b212c2SPaul Mackerras	addi	r4,r4,1
16594b212c2SPaul Mackerras	stb	r7,4(r6)
16694b212c2SPaul Mackerras	addi	r6,r6,1
16794b212c2SPaul Mackerras	bdnz	6b
16894b212c2SPaul Mackerras	subf	r5,r0,r5
16994b212c2SPaul Mackerras	rlwinm.	r7,r5,32-3,3,31
17094b212c2SPaul Mackerras	beq	2b
17194b212c2SPaul Mackerras	mtctr	r7
17294b212c2SPaul Mackerras	b	1b
17394b212c2SPaul Mackerras
17494b212c2SPaul Mackerras	.globl	backwards_memcpy
17594b212c2SPaul Mackerrasbackwards_memcpy:
17694b212c2SPaul Mackerras	rlwinm.	r7,r5,32-3,3,31		/* r7 = r5 >> 3 */
17794b212c2SPaul Mackerras	add	r6,r3,r5
17894b212c2SPaul Mackerras	add	r4,r4,r5
179b4e7de0fSPaul Mackerras	beq	3f
18094b212c2SPaul Mackerras	andi.	r0,r6,3
18194b212c2SPaul Mackerras	mtctr	r7
18294b212c2SPaul Mackerras	bne	5f
183b4e7de0fSPaul Mackerras	andi.	r0,r4,3
184b4e7de0fSPaul Mackerras	bne	3f
18594b212c2SPaul Mackerras1:	lwz	r7,-4(r4)
18694b212c2SPaul Mackerras	lwzu	r8,-8(r4)
18794b212c2SPaul Mackerras	stw	r7,-4(r6)
18894b212c2SPaul Mackerras	stwu	r8,-8(r6)
18994b212c2SPaul Mackerras	bdnz	1b
19094b212c2SPaul Mackerras	andi.	r5,r5,7
19194b212c2SPaul Mackerras2:	cmplwi	0,r5,4
19294b212c2SPaul Mackerras	blt	3f
19394b212c2SPaul Mackerras	lwzu	r0,-4(r4)
19494b212c2SPaul Mackerras	subi	r5,r5,4
19594b212c2SPaul Mackerras	stwu	r0,-4(r6)
19694b212c2SPaul Mackerras3:	cmpwi	0,r5,0
19794b212c2SPaul Mackerras	beqlr
19894b212c2SPaul Mackerras	mtctr	r5
19994b212c2SPaul Mackerras4:	lbzu	r0,-1(r4)
20094b212c2SPaul Mackerras	stbu	r0,-1(r6)
20194b212c2SPaul Mackerras	bdnz	4b
20294b212c2SPaul Mackerras	blr
203b4e7de0fSPaul Mackerras5:	cmpw	cr1,r0,r5
204b4e7de0fSPaul Mackerras	subf	r7,r0,r4
205b4e7de0fSPaul Mackerras	andi.	r7,r7,3
206b4e7de0fSPaul Mackerras	ble	cr1,3b
207b4e7de0fSPaul Mackerras	bne	3b
208b4e7de0fSPaul Mackerras	mtctr	r0
20994b212c2SPaul Mackerras6:	lbzu	r7,-1(r4)
21094b212c2SPaul Mackerras	stbu	r7,-1(r6)
21194b212c2SPaul Mackerras	bdnz	6b
21294b212c2SPaul Mackerras	subf	r5,r0,r5
21394b212c2SPaul Mackerras	rlwinm.	r7,r5,32-3,3,31
21494b212c2SPaul Mackerras	beq	2b
21594b212c2SPaul Mackerras	mtctr	r7
21694b212c2SPaul Mackerras	b	1b
21794b212c2SPaul Mackerras
2189b4b8febSDavid Gibson	.globl	memchr
2199b4b8febSDavid Gibsonmemchr:
2209b4b8febSDavid Gibson	cmpwi	0,r5,0
2219b4b8febSDavid Gibson	blelr
2229b4b8febSDavid Gibson	mtctr	r5
2239b4b8febSDavid Gibson	addi	r3,r3,-1
2249b4b8febSDavid Gibson1:	lbzu	r0,1(r3)
2259b4b8febSDavid Gibson	cmpw	r0,r4
2269b4b8febSDavid Gibson	beqlr
2279b4b8febSDavid Gibson	bdnz	1b
2289b4b8febSDavid Gibson	li	r3,0
2299b4b8febSDavid Gibson	blr
2309b4b8febSDavid Gibson
23194b212c2SPaul Mackerras	.globl	memcmp
23294b212c2SPaul Mackerrasmemcmp:
23394b212c2SPaul Mackerras	cmpwi	0,r5,0
23459fd6b8cSMike Ditto	ble	2f
23594b212c2SPaul Mackerras	mtctr	r5
23694b212c2SPaul Mackerras	addi	r6,r3,-1
23794b212c2SPaul Mackerras	addi	r4,r4,-1
23894b212c2SPaul Mackerras1:	lbzu	r3,1(r6)
23994b212c2SPaul Mackerras	lbzu	r0,1(r4)
24094b212c2SPaul Mackerras	subf.	r3,r0,r3
24194b212c2SPaul Mackerras	bdnzt	2,1b
24294b212c2SPaul Mackerras	blr
24359fd6b8cSMike Ditto2:	li	r3,0
24459fd6b8cSMike Ditto	blr
24594b212c2SPaul Mackerras
24694b212c2SPaul Mackerras
24794b212c2SPaul Mackerras/*
24894b212c2SPaul Mackerras * Flush the dcache and invalidate the icache for a range of addresses.
24994b212c2SPaul Mackerras *
25094b212c2SPaul Mackerras * flush_cache(addr, len)
25194b212c2SPaul Mackerras */
25294b212c2SPaul Mackerras	.global	flush_cache
25394b212c2SPaul Mackerrasflush_cache:
25494b212c2SPaul Mackerras	addi	4,4,0x1f	/* len = (len + 0x1f) / 0x20 */
25594b212c2SPaul Mackerras	rlwinm.	4,4,27,5,31
25694b212c2SPaul Mackerras	mtctr	4
25794b212c2SPaul Mackerras	beqlr
25894b212c2SPaul Mackerras1:	dcbf	0,3
25994b212c2SPaul Mackerras	icbi	0,3
26094b212c2SPaul Mackerras	addi	3,3,0x20
26194b212c2SPaul Mackerras	bdnz	1b
26294b212c2SPaul Mackerras	sync
26394b212c2SPaul Mackerras	isync
26494b212c2SPaul Mackerras	blr
26594b212c2SPaul Mackerras
266