⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uaccess.c

📁 Simple Operating Systems (简称SOS)是一个可以运行在X86平台上(包括QEMU
💻 C
字号:
/* Copyright (C) 2005  David Decotigny   This program is free software; you can redistribute it and/or   modify it under the terms of the GNU General Public License   as published by the Free Software Foundation; either version 2   of the License, or (at your option) any later version.      This program is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.      You should have received a copy of the GNU General Public License   along with this program; if not, write to the Free Software   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,   USA. */#include <hwcore/paging.h>#include <sos/thread.h>#include <sos/assert.h>#include <sos/kmalloc.h>#include "uaccess.h"int __uaccess_zero_fool_gcc = 0;/** * Make sure gcc optimizations don't go too far and don't suppress the * code at the given label because 1/ a global return is "above" (wrt * source code lines), 2/ it is never "goto" */#define KEEP_LABEL(lbl) \ ({ if (__uaccess_zero_fool_gcc) goto lbl; })static sos_ret_t nocheck_user_memcpy(sos_vaddr_t dest,				     sos_vaddr_t src,				     sos_size_t size,				     sos_bool_t transfer_from_user){  __label__ catch_pgflt;  char *cptr_dst;  const char *cptr_src;  sos_size_t transfer_sz;  sos_ret_t retval;  KEEP_LABEL(catch_pgflt);  if (size <= 0)    return 0;  retval = sos_thread_prepare_user_space_access(NULL,						(sos_vaddr_t) && catch_pgflt);  if (SOS_OK != retval)    return retval;  /* Version of memcpy that does not alter the stack pointer, in     order to be able to abruptedly jump to catch_pgflt without stack     inconsistency. That's the reason why we don't call the normal     memcpy here: on page fault, it would return back in here, with     the stack frame returning back here again */  for (cptr_dst = (char*)dest,	 cptr_src = (const char*)src,	 transfer_sz = size ;       transfer_sz > 0 ;       cptr_dst++, cptr_src++, transfer_sz--)    *cptr_dst = *cptr_src;  retval = sos_thread_end_user_space_access();  if (SOS_OK != retval)    return retval;  return size;  /* An unresolved page fault occured while accessing user space */ catch_pgflt:  {    struct sos_thread * cur_thr = sos_thread_get_current();    sos_uaddr_t faulted_uaddr = cur_thr->fixup_uaccess.faulted_uaddr;    if (transfer_from_user)      {	if ( (faulted_uaddr < src) || (faulted_uaddr - size > src) )	  sos_display_fatal_error("Unexpected read access in user space");	retval = faulted_uaddr - src;      }    else      {	if ( (faulted_uaddr < dest) || (faulted_uaddr - size > dest) )	  sos_display_fatal_error("Unexpected write access in user space");	retval = faulted_uaddr - dest;      }    sos_thread_end_user_space_access();    return retval;  }}sos_ret_t sos_memcpy_from_user(sos_vaddr_t kernel_to,			       sos_uaddr_t user_from,			       sos_size_t size){  /* Make sure user is trying to access user space */  if (user_from < SOS_PAGING_BASE_USER_ADDRESS)    return -SOS_EPERM;  if (user_from > SOS_PAGING_TOP_USER_ADDRESS - size)    return -SOS_EPERM;  return nocheck_user_memcpy(kernel_to, user_from, size, TRUE);}sos_ret_t sos_memdup_from_user(sos_vaddr_t * kernel_to, sos_uaddr_t from_user,			       sos_size_t length,			       sos_ui32_t kmalloc_flags){  sos_ret_t retval;  if (length <= 0)    return 0;  *kernel_to = sos_kmalloc(length, kmalloc_flags);  if (NULL == (void*) *kernel_to)    return -SOS_ENOMEM;  retval = sos_memcpy_from_user(*kernel_to, from_user, length);  if (length != retval)    {      sos_kfree((sos_vaddr_t)*kernel_to);      *kernel_to = (sos_vaddr_t) NULL;      retval = -SOS_EFAULT;    }  else    retval = SOS_OK;  return retval;}sos_ret_t sos_memcpy_to_user(sos_uaddr_t user_to,			     sos_vaddr_t kernel_from,			     sos_size_t size){  /* Make sure user is trying to access user space */  if (user_to < SOS_PAGING_BASE_USER_ADDRESS)    return -SOS_EPERM;  if (user_to > SOS_PAGING_TOP_USER_ADDRESS - size)    return -SOS_EPERM;  return nocheck_user_memcpy(user_to, kernel_from, size, FALSE);}sos_ret_t sos_strnlen_from_user(sos_uaddr_t user_str, sos_size_t max_len){  __label__ catch_pgflt;  const char *sc;  sos_ret_t retval;  KEEP_LABEL(catch_pgflt);  if (max_len <= 0)    return 0;  /* Make sure user is trying to access user space */  if (user_str < SOS_PAGING_BASE_USER_ADDRESS)    return -SOS_EPERM;  /* Don't allow invalid max_len */  if ( (max_len < 1) || (max_len > SOS_PAGING_USER_SPACE_SIZE) )    return -SOS_EINVAL;  retval = sos_thread_prepare_user_space_access(NULL,						(sos_vaddr_t) && catch_pgflt);  if (SOS_OK != retval)    return retval;  /* Version of strnlen that does not alter the stack pointer, in     order to be able to abruptedly jump to catch_pgflt without stack     inconsistency. That's the reason why we don't call the normal     strnlen here: on page fault, it would return back in here, with     the stack frame returning back here again */  for (sc = (const char *)user_str; max_len-- && *sc != '\0'; ++sc)    continue;  retval = sos_thread_end_user_space_access();  if (SOS_OK != retval)    return retval;  return ((sos_uaddr_t)sc - user_str);  /* An unresolved page fault occured while accessing user space */ catch_pgflt:  {    sos_thread_end_user_space_access();    return -SOS_EFAULT;  }}static sos_ret_t nocheck_user_strzcpy(char *dst, const char *src,				      sos_size_t len){  __label__ catch_pgflt;  unsigned int i;  sos_ret_t retval;  KEEP_LABEL(catch_pgflt);  if (len <= 0)    return 0;  retval = sos_thread_prepare_user_space_access(NULL,						(sos_vaddr_t) && catch_pgflt);  if (SOS_OK != retval)    return retval;  /* Version of strzcpy that does not alter the stack pointer, in     order to be able to abruptedly jump to catch_pgflt without stack     inconsistency. That's the reason why we don't call the normal     strzcpy here: on page fault, it would return back in here, with     the stack frame returning back here again */  for (i = 0; i < len; i++)    {      dst[i] = src[i];      if(src[i] == '\0')        break;    }    dst[len-1] = '\0';   retval = sos_thread_end_user_space_access();  if (SOS_OK != retval)    return retval;  return SOS_OK;  /* An unresolved page fault occured while accessing user space */ catch_pgflt:  {    sos_thread_end_user_space_access();    return -SOS_EFAULT;  }}sos_ret_t sos_strzcpy_from_user(char *kernel_to, sos_uaddr_t user_from,				sos_size_t max_len){  /* Make sure user is trying to access user space */  if ((sos_uaddr_t)user_from < SOS_PAGING_BASE_USER_ADDRESS)    return -SOS_EPERM;  /* Don't allow invalid max_len */  if ( (max_len < 1) || (max_len > SOS_PAGING_USER_SPACE_SIZE) )    return -SOS_EINVAL;  return nocheck_user_strzcpy(kernel_to, (const char*)user_from, max_len);}sos_ret_t sos_strzcpy_to_user(sos_uaddr_t user_to, const char *kernel_from,			      sos_size_t max_len){  /* Make sure user is trying to access user space */  if ((sos_uaddr_t)user_to < SOS_PAGING_BASE_USER_ADDRESS)    return -SOS_EPERM;  /* Don't allow invalid max_len */  if ( (max_len < 1) || (max_len > SOS_PAGING_USER_SPACE_SIZE) )    return -SOS_EINVAL;  return nocheck_user_strzcpy((char*)user_to, kernel_from, max_len);}sos_ret_t sos_strndup_from_user(char ** kernel_to, sos_uaddr_t from_user,				sos_size_t max_len,				sos_ui32_t kmalloc_flags){  sos_ret_t retval = sos_strnlen_from_user(from_user, max_len);  if (retval < 0)    return retval;  *kernel_to = (char*)sos_kmalloc(retval + 1, kmalloc_flags);  if (NULL == *kernel_to)    return -SOS_ENOMEM;  retval = sos_strzcpy_from_user(*kernel_to, from_user, retval + 1);  if (SOS_OK != retval)    {      sos_kfree((sos_vaddr_t)*kernel_to);      *kernel_to = NULL;    }  return retval;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -