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