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