xref: /xnu-10063.141.1/osfmk/arm64/bzero.s (revision d8b80295118ef25ac3a784134bcf95cd8e88109f)
1*d8b80295SApple OSS Distributions/*
2*d8b80295SApple OSS Distributions * Copyright (c) 2012 Apple Computer, Inc. All rights reserved.
3*d8b80295SApple OSS Distributions *
4*d8b80295SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*d8b80295SApple OSS Distributions *
6*d8b80295SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*d8b80295SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*d8b80295SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*d8b80295SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*d8b80295SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*d8b80295SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*d8b80295SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*d8b80295SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*d8b80295SApple OSS Distributions *
15*d8b80295SApple OSS Distributions * Please obtain a copy of the License at
16*d8b80295SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*d8b80295SApple OSS Distributions *
18*d8b80295SApple OSS Distributions * The Original Code and all software distributed under the License are
19*d8b80295SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*d8b80295SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*d8b80295SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*d8b80295SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*d8b80295SApple OSS Distributions * Please see the License for the specific language governing rights and
24*d8b80295SApple OSS Distributions * limitations under the License.
25*d8b80295SApple OSS Distributions *
26*d8b80295SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*d8b80295SApple OSS Distributions *
28*d8b80295SApple OSS Distributions * This file implements the following functions for the arm64 architecture:
29*d8b80295SApple OSS Distributions *
30*d8b80295SApple OSS Distributions *  void bzero(void *buffer, size_t length);
31*d8b80295SApple OSS Distributions *  void __bzero(void *buffer, size_t length);
32*d8b80295SApple OSS Distributions *  void *memset(void *buffer, int value, size_t length);
33*d8b80295SApple OSS Distributions *
34*d8b80295SApple OSS Distributions * The first two zero-fill a buffer.  The third fills the buffer with the low
35*d8b80295SApple OSS Distributions * byte of its second argument.
36*d8b80295SApple OSS Distributions */
37*d8b80295SApple OSS Distributions
38*d8b80295SApple OSS Distributions#include "asm.h"
39*d8b80295SApple OSS Distributions
40*d8b80295SApple OSS Distributions.globl _bzero
41*d8b80295SApple OSS Distributions.globl ___bzero
42*d8b80295SApple OSS Distributions.globl _memset
43*d8b80295SApple OSS Distributions.globl _secure_memset
44*d8b80295SApple OSS Distributions
45*d8b80295SApple OSS Distributions/*****************************************************************************
46*d8b80295SApple OSS Distributions *  bzero entrypoint                                                         *
47*d8b80295SApple OSS Distributions *****************************************************************************/
48*d8b80295SApple OSS Distributions
49*d8b80295SApple OSS Distributions.text
50*d8b80295SApple OSS Distributions.align 4
51*d8b80295SApple OSS Distributions_bzero:
52*d8b80295SApple OSS Distributions___bzero:
53*d8b80295SApple OSS Distributions    ARM64_STACK_PROLOG
54*d8b80295SApple OSS Distributions    PUSH_FRAME
55*d8b80295SApple OSS Distributions    mov     x2,      x1
56*d8b80295SApple OSS Distributions    eor     x1,      x1, x1
57*d8b80295SApple OSS Distributions    mov     x3,      x0
58*d8b80295SApple OSS Distributions    cmp     x2,      #128
59*d8b80295SApple OSS Distributions    b.cc    L_memsetSmall
60*d8b80295SApple OSS Distributions
61*d8b80295SApple OSS Distributions/*****************************************************************************
62*d8b80295SApple OSS Distributions *  Large buffer zero engine                                                 *
63*d8b80295SApple OSS Distributions *****************************************************************************/
64*d8b80295SApple OSS Distributions
65*d8b80295SApple OSS DistributionsL_bzeroLarge:
66*d8b80295SApple OSS Distributions//  Write the first 64 bytes of the buffer without regard to alignment, then
67*d8b80295SApple OSS Distributions//  advance x3 to point to a cacheline-aligned location within the buffer, and
68*d8b80295SApple OSS Distributions//  decrement the length accordingly.
69*d8b80295SApple OSS Distributions    stp     x1, x1, [x0]
70*d8b80295SApple OSS Distributions    stp     x1, x1, [x0, #16]
71*d8b80295SApple OSS Distributions    stp     x1, x1, [x0, #32]
72*d8b80295SApple OSS Distributions    stp     x1, x1, [x0, #48]
73*d8b80295SApple OSS Distributions    add     x3,      x0, #64
74*d8b80295SApple OSS Distributions    and     x3,      x3, #-64
75*d8b80295SApple OSS Distributions    add     x2,      x2, x0   // end of buffer
76*d8b80295SApple OSS Distributions    add     x4,      x3, #64  // end of first cacheline to zero
77*d8b80295SApple OSS Distributions    subs    x2,      x2, x4   // if the end of the buffer comes first, jump
78*d8b80295SApple OSS Distributions    b.ls    1f                //    directly to the cleanup pass.
79*d8b80295SApple OSS Distributions0:  dc      zva,     x3       // zero cacheline
80*d8b80295SApple OSS Distributions    add     x3,      x3, #64  // increment pointer
81*d8b80295SApple OSS Distributions    subs    x2,      x2, #64  // decrement length
82*d8b80295SApple OSS Distributions    b.hi    0b
83*d8b80295SApple OSS Distributions1:  add     x3,      x3, x2   // back up pointer to (end of buffer) - 64.
84*d8b80295SApple OSS Distributions    stp     x1, x1, [x3]      // and store 64 bytes to reach end of buffer.
85*d8b80295SApple OSS Distributions    stp     x1, x1, [x3, #16]
86*d8b80295SApple OSS Distributions    stp     x1, x1, [x3, #32]
87*d8b80295SApple OSS Distributions    stp     x1, x1, [x3, #48]
88*d8b80295SApple OSS Distributions    POP_FRAME
89*d8b80295SApple OSS Distributions    ARM64_STACK_EPILOG
90*d8b80295SApple OSS Distributions
91*d8b80295SApple OSS Distributions/*****************************************************************************
92*d8b80295SApple OSS Distributions *  memset entrypoint                                                        *
93*d8b80295SApple OSS Distributions *****************************************************************************/
94*d8b80295SApple OSS Distributions
95*d8b80295SApple OSS Distributions.align 4
96*d8b80295SApple OSS Distributions/*
97*d8b80295SApple OSS Distributions * It is important that secure_memset remains defined in assembly to avoid
98*d8b80295SApple OSS Distributions * compiler optimizations.
99*d8b80295SApple OSS Distributions */
100*d8b80295SApple OSS Distributions_secure_memset:
101*d8b80295SApple OSS Distributions_memset:
102*d8b80295SApple OSS Distributions    ARM64_STACK_PROLOG
103*d8b80295SApple OSS Distributions    PUSH_FRAME
104*d8b80295SApple OSS Distributions    and     x1,      x1, #0xff
105*d8b80295SApple OSS Distributions    orr     x3,      xzr,#0x0101010101010101
106*d8b80295SApple OSS Distributions    mul     x1,      x1, x3
107*d8b80295SApple OSS Distributions    mov     x3,      x0
108*d8b80295SApple OSS Distributions    cmp     x2,      #64
109*d8b80295SApple OSS Distributions    b.cc    L_memsetSmall
110*d8b80295SApple OSS Distributions
111*d8b80295SApple OSS Distributions/*****************************************************************************
112*d8b80295SApple OSS Distributions *  Large buffer store engine                                                *
113*d8b80295SApple OSS Distributions *****************************************************************************/
114*d8b80295SApple OSS Distributions
115*d8b80295SApple OSS DistributionsL_memsetLarge:
116*d8b80295SApple OSS Distributions//  Write the first 64 bytes of the buffer without regard to alignment, then
117*d8b80295SApple OSS Distributions//  advance x3 to point to an aligned location within the buffer, and
118*d8b80295SApple OSS Distributions//  decrement the length accordingly.
119*d8b80295SApple OSS Distributions    stp     x1, x1, [x0]
120*d8b80295SApple OSS Distributions    add     x3,      x0, #16
121*d8b80295SApple OSS Distributions    and     x3,      x3, #-16
122*d8b80295SApple OSS Distributions    add     x2,      x2, x0   // end of buffer
123*d8b80295SApple OSS Distributions    add     x4,      x3, #64  // end of first aligned 64-byte store
124*d8b80295SApple OSS Distributions    subs    x2,      x2, x4   // if the end of the buffer comes first, jump
125*d8b80295SApple OSS Distributions    b.ls    1f                //    directly to the cleanup store.
126*d8b80295SApple OSS Distributions0:  stnp    x1, x1, [x3]
127*d8b80295SApple OSS Distributions    stnp    x1, x1, [x3, #16]
128*d8b80295SApple OSS Distributions    stnp    x1, x1, [x3, #32]
129*d8b80295SApple OSS Distributions    stnp    x1, x1, [x3, #48]
130*d8b80295SApple OSS Distributions    add     x3,      x3, #64
131*d8b80295SApple OSS Distributions    subs    x2,      x2, #64
132*d8b80295SApple OSS Distributions    b.hi    0b
133*d8b80295SApple OSS Distributions1:  add     x3,      x3, x2   // back up pointer to (end of buffer) - 64.
134*d8b80295SApple OSS Distributions    stp     x1, x1, [x3]
135*d8b80295SApple OSS Distributions    stp     x1, x1, [x3, #16]
136*d8b80295SApple OSS Distributions    stp     x1, x1, [x3, #32]
137*d8b80295SApple OSS Distributions    stp     x1, x1, [x3, #48]
138*d8b80295SApple OSS Distributions    POP_FRAME
139*d8b80295SApple OSS Distributions    ARM64_STACK_EPILOG
140*d8b80295SApple OSS Distributions
141*d8b80295SApple OSS Distributions/*****************************************************************************
142*d8b80295SApple OSS Distributions *  Small buffer store engine                                                *
143*d8b80295SApple OSS Distributions *****************************************************************************/
144*d8b80295SApple OSS Distributions
145*d8b80295SApple OSS Distributions0:  str     x1,     [x3],#8
146*d8b80295SApple OSS DistributionsL_memsetSmall:
147*d8b80295SApple OSS Distributions    subs    x2,      x2, #8
148*d8b80295SApple OSS Distributions    b.cs    0b
149*d8b80295SApple OSS Distributions    adds    x2,      x2, #8
150*d8b80295SApple OSS Distributions    b.eq    2f
151*d8b80295SApple OSS Distributions1:  strb    w1,     [x3],#1
152*d8b80295SApple OSS Distributions    subs    x2,      x2, #1
153*d8b80295SApple OSS Distributions    b.ne    1b
154*d8b80295SApple OSS Distributions2:  POP_FRAME
155*d8b80295SApple OSS Distributions    ARM64_STACK_EPILOG
156*d8b80295SApple OSS Distributions
157