xref: /linux/arch/s390/lib/mem.S (revision f3a8b6645dc2e60d11f20c1c23afd964ff4e55ae)
1/*
2 * String handling functions.
3 *
4 * Copyright IBM Corp. 2012
5 */
6
7#include <linux/linkage.h>
8#include <asm/export.h>
9
10/*
11 * memset implementation
12 *
13 * This code corresponds to the C construct below. We do distinguish
14 * between clearing (c == 0) and setting a memory array (c != 0) simply
15 * because nearly all memset invocations in the kernel clear memory and
16 * the xc instruction is preferred in such cases.
17 *
18 * void *memset(void *s, int c, size_t n)
19 * {
20 *	if (likely(c == 0))
21 *		return __builtin_memset(s, 0, n);
22 *	return __builtin_memset(s, c, n);
23 * }
24 */
25ENTRY(memset)
26	ltgr	%r4,%r4
27	bzr	%r14
28	ltgr	%r3,%r3
29	jnz	.Lmemset_fill
30	aghi	%r4,-1
31	srlg	%r3,%r4,8
32	ltgr	%r3,%r3
33	lgr	%r1,%r2
34	jz	.Lmemset_clear_rest
35.Lmemset_clear_loop:
36	xc	0(256,%r1),0(%r1)
37	la	%r1,256(%r1)
38	brctg	%r3,.Lmemset_clear_loop
39.Lmemset_clear_rest:
40	larl	%r3,.Lmemset_xc
41	ex	%r4,0(%r3)
42	br	%r14
43.Lmemset_fill:
44	stc	%r3,0(%r2)
45	cghi	%r4,1
46	lgr	%r1,%r2
47	ber	%r14
48	aghi	%r4,-2
49	srlg	%r3,%r4,8
50	ltgr	%r3,%r3
51	jz	.Lmemset_fill_rest
52.Lmemset_fill_loop:
53	mvc	1(256,%r1),0(%r1)
54	la	%r1,256(%r1)
55	brctg	%r3,.Lmemset_fill_loop
56.Lmemset_fill_rest:
57	larl	%r3,.Lmemset_mvc
58	ex	%r4,0(%r3)
59	br	%r14
60.Lmemset_xc:
61	xc	0(1,%r1),0(%r1)
62.Lmemset_mvc:
63	mvc	1(1,%r1),0(%r1)
64EXPORT_SYMBOL(memset)
65
66/*
67 * memcpy implementation
68 *
69 * void *memcpy(void *dest, const void *src, size_t n)
70 */
71ENTRY(memcpy)
72	ltgr	%r4,%r4
73	bzr	%r14
74	aghi	%r4,-1
75	srlg	%r5,%r4,8
76	ltgr	%r5,%r5
77	lgr	%r1,%r2
78	jnz	.Lmemcpy_loop
79.Lmemcpy_rest:
80	larl	%r5,.Lmemcpy_mvc
81	ex	%r4,0(%r5)
82	br	%r14
83.Lmemcpy_loop:
84	mvc	0(256,%r1),0(%r3)
85	la	%r1,256(%r1)
86	la	%r3,256(%r3)
87	brctg	%r5,.Lmemcpy_loop
88	j	.Lmemcpy_rest
89.Lmemcpy_mvc:
90	mvc	0(1,%r1),0(%r3)
91EXPORT_SYMBOL(memcpy)
92