user: add wrappers for userspace data transfer

This doesn't do anything right now, but will save some work in the
future when we have hardware memory protection.
This commit is contained in:
Felix Kopp 2021-01-05 13:48:39 +01:00
parent 1c835a9738
commit 51c9a779a3
No known key found for this signature in database
GPG key ID: C478BA0A85F75728
3 changed files with 116 additions and 0 deletions

53
include/ardix/userspace.h Normal file
View file

@ -0,0 +1,53 @@
/* SPDX-License-Identifier: BSD-3-Clause */
/* See the end of this file for copyright, licensing, and warranty information. */
#pragma once
#include <ardix/types.h>
#include <toolchain.h>
/**
* Copy data from user space to kernel space.
*
* @param dest: where to copy to
* @param src: where to copy from
* @param len: amount of bytes to copy
* @returns amount of bytes copied
*/
size_t copy_from_user(void *dest, __user const void *src, size_t len);
/**
* Copy data from kernel space to user space.
*
* @param dest: where to copy to
* @param src: where to copy from
* @param len: amount of bytes to copy
* @returns amount of bytes copied
*/
size_t copy_to_user(__user void *dest, __user const void *src, size_t len);
/*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* 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.
* 3. Neither the name of the copyright holder nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR
* CONTRIBUTORS 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.
*/

View file

@ -46,8 +46,20 @@
#define __section(name) __attribute__((section(#name)))
#endif /* __section */
#ifndef __rodata
/** Place a variable in program memory rather than into RAM. */
#define __rodata __section(.rodata#)
#endif
#ifndef __user
/** Denote a pointer to user space (this will be used for static code checks later) */
#define __user
#endif
#ifndef __shared
/** Storage attribute indicating the symbol will be shared with userspace. */
#define __shared __section(.text.shared)
#endif
/*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>

51
kernel/userspace.c Normal file
View file

@ -0,0 +1,51 @@
/* SPDX-License-Identifier: BSD-3-Clause */
/* See the end of this file for copyright, licensing, and warranty information. */
#include <ardix/types.h>
#include <ardix/userspace.h>
#include <ardix/string.h>
#include <toolchain.h>
/*
* These don't do anything special because there is no MPU or other protection
* yet, but having them as a wrapper this early is probably a good idea because
* it make life easier when the MPU is active.
*/
size_t copy_from_user(void *dest, __user const void *src, size_t len)
{
void *tmp = memcpy(dest, src, len);
return (size_t)tmp - (size_t)dest;
}
size_t copy_to_user(__user void *dest, const void *src, size_t len)
{
void *tmp = memcpy(dest, src, len);
return (size_t)tmp - (size_t)dest;
}
/*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* 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.
* 3. Neither the name of the copyright holder nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR
* CONTRIBUTORS 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.
*/