From 51c9a779a391a1e22a5cba9822ef8a2de3ca716a Mon Sep 17 00:00:00 2001 From: Felix Kopp Date: Tue, 5 Jan 2021 13:48:39 +0100 Subject: [PATCH] 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. --- include/ardix/userspace.h | 53 +++++++++++++++++++++++++++++++++++++++ include/toolchain.h | 12 +++++++++ kernel/userspace.c | 51 +++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 include/ardix/userspace.h create mode 100644 kernel/userspace.c diff --git a/include/ardix/userspace.h b/include/ardix/userspace.h new file mode 100644 index 0000000..faaf6b8 --- /dev/null +++ b/include/ardix/userspace.h @@ -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 + +#include + +/** + * 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 + * + * 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. + */ diff --git a/include/toolchain.h b/include/toolchain.h index b4f2dac..b455f9d 100644 --- a/include/toolchain.h +++ b/include/toolchain.h @@ -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 diff --git a/kernel/userspace.c b/kernel/userspace.c new file mode 100644 index 0000000..527239c --- /dev/null +++ b/kernel/userspace.c @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* See the end of this file for copyright, licensing, and warranty information. */ + +#include +#include + +#include +#include + +/* + * 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 + * + * 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. + */