#include <mach_assert.h>
#include <sys/errno.h>
#include <i386/param.h>
#include <i386/misc_protos.h>
#include <i386/cpu_data.h>
#include <i386/machine_routines.h>
#include <i386/cpuid.h>
#include <i386/vmx.h>
#include <vm/pmap.h>
#include <vm/vm_map.h>
#include <vm/vm_kern.h>
#include <vm/vm_fault.h>
#include <sys/kdebug.h>
static int copyio(int, user_addr_t, char *, vm_size_t, vm_size_t *, int);
static int copyio_phys(addr64_t, addr64_t, vm_size_t, int);
#define COPYSIZELIMIT_PANIC (64*MB)
extern int _bcopy(const void *, void *, vm_size_t);
extern int _bcopystr(const void *, void *, vm_size_t, vm_size_t *);
#define COPYIN 0
#define COPYOUT 1
#define COPYINSTR 2
#define COPYINPHYS 3
#define COPYOUTPHYS 4
#if DEVELOPMENT
typedef struct {
uint64_t timestamp;
thread_t thread;
uintptr_t cr4;
uint8_t cpuid;
uint8_t smap_state;
uint8_t copyio_active;
} smaplog_entry_t;
#define SMAPLOG_BUFFER_SIZE (50)
static smaplog_entry_t smaplog_cbuf[SMAPLOG_BUFFER_SIZE];
static uint32_t smaplog_head = 0;
static void
smaplog_add_entry(boolean_t enabling)
{
uint32_t index = 0;
thread_t thread = current_thread();
do {
index = smaplog_head;
} while (!OSCompareAndSwap(index, (index + 1) % SMAPLOG_BUFFER_SIZE, &smaplog_head));
assert(index < SMAPLOG_BUFFER_SIZE);
assert(smaplog_head < SMAPLOG_BUFFER_SIZE);
assert(thread);
smaplog_cbuf[index].timestamp = mach_absolute_time();
smaplog_cbuf[index].thread = thread;
smaplog_cbuf[index].cpuid = cpu_number();
smaplog_cbuf[index].cr4 = get_cr4();
smaplog_cbuf[index].smap_state = enabling;
smaplog_cbuf[index].copyio_active = (thread->machine.specFlags & CopyIOActive) ? 1 : 0;
}
#endif
extern boolean_t pmap_smap_enabled;
static inline void user_access_enable(void) {
if (pmap_smap_enabled) {
stac();
#if DEVELOPMENT
smaplog_add_entry(TRUE);
#endif
}
}
static inline void user_access_disable(void) {
if (pmap_smap_enabled) {
clac();
#if DEVELOPMENT
smaplog_add_entry(FALSE);
#endif
}
}
static int
copyio(int copy_type, user_addr_t user_addr, char *kernel_addr,
vm_size_t nbytes, vm_size_t *lencopied, int use_kernel_map)
{
thread_t thread;
pmap_t pmap;
vm_size_t bytes_copied;
int error = 0;
boolean_t istate = FALSE;
boolean_t recursive_CopyIOActive;
#if KDEBUG
int debug_type = 0xeff70010;
debug_type += (copy_type << 2);
#endif
assert(nbytes < COPYSIZELIMIT_PANIC);
thread = current_thread();
KERNEL_DEBUG(debug_type | DBG_FUNC_START,
(unsigned)(user_addr >> 32), (unsigned)user_addr,
nbytes, thread->machine.copyio_state, 0);
if (nbytes == 0)
goto out;
pmap = thread->map->pmap;
if ((copy_type != COPYINPHYS) && (copy_type != COPYOUTPHYS) && ((vm_offset_t)kernel_addr < VM_MIN_KERNEL_AND_KEXT_ADDRESS)) {
panic("Invalid copy parameter, copy type: %d, kernel address: %p", copy_type, kernel_addr);
}
if (((pmap != kernel_pmap) && (use_kernel_map == 0)) &&
((nbytes && (user_addr+nbytes <= user_addr)) || ((user_addr + nbytes) > vm_map_max(thread->map)))) {
error = EFAULT;
goto out;
}
recursive_CopyIOActive = thread->machine.specFlags & CopyIOActive;
thread->machine.specFlags |= CopyIOActive;
user_access_enable();
if (no_shared_cr3) {
istate = ml_set_interrupts_enabled(FALSE);
if (get_cr3_base() != pmap->pm_cr3)
set_cr3_raw(pmap->pm_cr3);
}
if ((pmap != kernel_pmap) && !use_kernel_map &&
(get_cr3_base() != pmap->pm_cr3)) {
panic("copyio(%d,%p,%p,%ld,%p,%d) cr3 is %p expects %p",
copy_type, (void *)user_addr, kernel_addr, nbytes, lencopied, use_kernel_map,
(void *) get_cr3_raw(), (void *) pmap->pm_cr3);
}
if (no_shared_cr3)
(void) ml_set_interrupts_enabled(istate);
KERNEL_DEBUG(0xeff70044 | DBG_FUNC_NONE, (unsigned)user_addr,
(unsigned)kernel_addr, nbytes, 0, 0);
switch (copy_type) {
case COPYIN:
error = _bcopy((const void *) user_addr,
kernel_addr,
nbytes);
break;
case COPYOUT:
error = _bcopy(kernel_addr,
(void *) user_addr,
nbytes);
break;
case COPYINPHYS:
error = _bcopy((const void *) user_addr,
PHYSMAP_PTOV(kernel_addr),
nbytes);
break;
case COPYOUTPHYS:
error = _bcopy((const void *) PHYSMAP_PTOV(kernel_addr),
(void *) user_addr,
nbytes);
break;
case COPYINSTR:
error = _bcopystr((const void *) user_addr,
kernel_addr,
(int) nbytes,
&bytes_copied);
if (error != EFAULT)
*lencopied = bytes_copied;
if (error) {
#if KDEBUG
nbytes = *lencopied;
#endif
break;
}
if (*(kernel_addr + bytes_copied - 1) == 0) {
#if KDEBUG
nbytes = *lencopied;
#endif
break;
} else {
#if KDEBUG
nbytes = *lencopied;
#endif
error = ENAMETOOLONG;
break;
}
break;
}
user_access_disable();
if (!recursive_CopyIOActive) {
thread->machine.specFlags &= ~CopyIOActive;
}
if (no_shared_cr3) {
istate = ml_set_interrupts_enabled(FALSE);
if (get_cr3_raw() != kernel_pmap->pm_cr3)
set_cr3_raw(kernel_pmap->pm_cr3);
(void) ml_set_interrupts_enabled(istate);
}
out:
KERNEL_DEBUG(debug_type | DBG_FUNC_END, (unsigned)user_addr,
(unsigned)kernel_addr, (unsigned)nbytes, error, 0);
return (error);
}
static int
copyio_phys(addr64_t source, addr64_t sink, vm_size_t csize, int which)
{
char *paddr;
user_addr_t vaddr;
int ctype;
if (which & cppvPsnk) {
paddr = (char *)sink;
vaddr = (user_addr_t)source;
ctype = COPYINPHYS;
} else {
paddr = (char *)source;
vaddr = (user_addr_t)sink;
ctype = COPYOUTPHYS;
}
return copyio(ctype, vaddr, paddr, csize, NULL, which & cppvKmap);
}
int
copyinmsg(const user_addr_t user_addr, char *kernel_addr, mach_msg_size_t nbytes)
{
return copyio(COPYIN, user_addr, kernel_addr, nbytes, NULL, 0);
}
int
copyin(const user_addr_t user_addr, char *kernel_addr, vm_size_t nbytes)
{
return copyio(COPYIN, user_addr, kernel_addr, nbytes, NULL, 0);
}
int
copyinstr(const user_addr_t user_addr, char *kernel_addr, vm_size_t nbytes, vm_size_t *lencopied)
{
*lencopied = 0;
return copyio(COPYINSTR, user_addr, kernel_addr, nbytes, lencopied, 0);
}
int
copyoutmsg(const char *kernel_addr, user_addr_t user_addr, mach_msg_size_t nbytes)
{
return copyio(COPYOUT, user_addr, (char *)(uintptr_t)kernel_addr, nbytes, NULL, 0);
}
int
copyout(const void *kernel_addr, user_addr_t user_addr, vm_size_t nbytes)
{
return copyio(COPYOUT, user_addr, (char *)(uintptr_t)kernel_addr, nbytes, NULL, 0);
}
kern_return_t
copypv(addr64_t src64, addr64_t snk64, unsigned int size, int which)
{
unsigned int lop, csize;
int bothphys = 0;
KERNEL_DEBUG(0xeff7004c | DBG_FUNC_START, (unsigned)src64,
(unsigned)snk64, size, which, 0);
if ((which & (cppvPsrc | cppvPsnk)) == 0 )
panic("copypv: no more than 1 parameter may be virtual\n");
if ((which & (cppvPsrc | cppvPsnk)) == (cppvPsrc | cppvPsnk))
bothphys = 1;
while (size) {
if (bothphys) {
lop = (unsigned int)(PAGE_SIZE - (snk64 & (PAGE_SIZE - 1)));
if (lop > (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1))))
lop = (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1)));
} else {
if (which & cppvPsrc)
lop = (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1)));
else
lop = (unsigned int)(PAGE_SIZE - (snk64 & (PAGE_SIZE - 1)));
}
csize = size;
if (lop < size)
csize = lop;
#if 0
if (which & cppvFsrc)
flush_dcache64(src64, csize, 1);
if (which & cppvFsnk)
flush_dcache64(snk64, csize, 1);
#endif
if (bothphys)
bcopy_phys(src64, snk64, csize);
else {
if (copyio_phys(src64, snk64, csize, which))
return (KERN_FAILURE);
}
#if 0
if (which & cppvFsrc)
flush_dcache64(src64, csize, 1);
if (which & cppvFsnk)
flush_dcache64(snk64, csize, 1);
#endif
size -= csize;
snk64 += csize;
src64 += csize;
}
KERNEL_DEBUG(0xeff7004c | DBG_FUNC_END, (unsigned)src64,
(unsigned)snk64, size, which, 0);
return KERN_SUCCESS;
}