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