/*
* Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net
* All rights reserved.
*
* This code is part of the KASAN subsystem of the NetBSD kernel.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
if (__predict_false(__md_early)) {
__md_early_shadow_map_page(va);
return;
}
if (!pmap_valid_entry(L4_BASE[pl4_i(va)])) {
pa = __md_palloc();
L4_BASE[pl4_i(va)] = pa | pteflags;
}
if (!pmap_valid_entry(L3_BASE[pl3_i(va)])) {
pa = __md_palloc();
L3_BASE[pl3_i(va)] = pa | pteflags;
}
if (!pmap_valid_entry(L2_BASE[pl2_i(va)])) {
if ((pa = __md_palloc_large()) != 0) {
L2_BASE[pl2_i(va)] = pa | pteflags | PTE_PS |
pmap_pg_g;
__insn_barrier();
__builtin_memset((void *)va, 0, NBPD_L2);
return;
}
pa = __md_palloc();
L2_BASE[pl2_i(va)] = pa | pteflags;
} else if (L2_BASE[pl2_i(va)] & PTE_PS) {
return;
}
if (!pmap_valid_entry(L1_BASE[pl1_i(va)])) {
pa = __md_palloc();
L1_BASE[pl1_i(va)] = pa | pteflags | pmap_pg_g;
}
}
/*
* Map only the current stack. We will map the rest in kasan_init.
*/
static void
kasan_md_early_init(void *stack)
{
kasan_shadow_map(stack, USPACE);
__md_early = false;
}
/*
* Create the shadow mapping. We don't create the 'User' area, because we
* exclude it from the monitoring. The 'Main' area is created dynamically
* in pmap_growkernel.
*/
static void
kasan_md_init(void)
{
extern struct bootspace bootspace;
size_t i;