C os161中userptr\t类型的用途是什么?
我正在努力完成操作系统课程的作业 我在作业中有一个问题: userptr\t的目的是什么 当我在源代码中搜索C os161中userptr\t类型的用途是什么?,c,operating-system,kernel,os161,C,Operating System,Kernel,Os161,我正在努力完成操作系统课程的作业 我在作业中有一个问题: userptr\t的目的是什么 当我在源代码中搜索userptr\u t时,我发现: /* * Define userptr_t as a pointer to a one-byte struct, so it won't mix * with other pointers. */ struct __userptr { char _dummy; }; typedef struct __userptr *userptr_t; typ
userptr\u t
时,我发现:
/*
* Define userptr_t as a pointer to a one-byte struct, so it won't mix
* with other pointers.
*/
struct __userptr { char _dummy; };
typedef struct __userptr *userptr_t;
typedef const struct __userptr *const_userptr_t;
我无法完全理解它的用途,有人能解释一下这种类型的用途吗
例如,在函数copyin
,copyinstr
,copyinstr
,copyinstr
,copyinstr
和其他函数中的文件copyinout.c
中使用了它:
#include <types.h>
#include <kern/errno.h>
#include <lib.h>
#include <setjmp.h>
#include <thread.h>
#include <current.h>
#include <vm.h>
#include <copyinout.h>
/*
* User/kernel memory copying functions.
*
* These are arranged to prevent fatal kernel memory faults if invalid
* addresses are supplied by user-level code. This code is itself
* machine-independent; it uses the machine-dependent C setjmp/longjmp
* facility to perform recovery.
*
* However, it assumes things about the memory subsystem that may not
* be true on all platforms.
*
* (1) It assumes that user memory is mapped into the current address
* space while running in the kernel, and can be accessed by just
* dereferencing a pointer in the ordinary way. (And not, for example,
* with special instructions or via special segment registers.)
*
* (2) It assumes that the user-space region of memory is contiguous
* and extends from 0 to some virtual address USERSPACETOP, and so if
* a user process passes a kernel address the logic in copycheck()
* will trap it.
*
* (3) It assumes that access to user memory from the kernel behaves
* the same way as access to user memory from user space: for
* instance, that the processor honors read-only bits on memory pages
* when in kernel mode.
*
* (4) It assumes that if a proper user-space address that is valid
* but not present, or not valid at all, is touched from the kernel,
* that the correct faults will occur and the VM system will load the
* necessary pages and whatnot.
*
* (5) It assumes that the machine-dependent trap logic provides and
* honors a tm_badfaultfunc field in the thread_machdep structure.
* This feature works as follows: if an otherwise fatal fault occurs
* in kernel mode, and tm_badfaultfunc is set, execution resumes in
* the function pointed to by tm_badfaultfunc.
*
* This code works by setting tm_badfaultfunc and then copying memory
* in an ordinary fashion. If these five assumptions are satisfied,
* which is the case for many ordinary CPU types, this code should
* function correctly. If the assumptions are not satisfied on some
* platform (for instance, certain old 80386 processors violate
* assumption 3), this code cannot be used, and cpu- or platform-
* specific code must be written.
*
* To make use of this code, in addition to tm_badfaultfunc the
* thread_machdep structure should contain a jmp_buf called
* "tm_copyjmp".
*/
/*
* Recovery function. If a fatal fault occurs during copyin, copyout,
* copyinstr, or copyoutstr, execution resumes here. (This behavior is
* caused by setting t_machdep.tm_badfaultfunc and is implemented in
* machine-dependent code.)
*
* We use the C standard function longjmp() to teleport up the call
* stack to where setjmp() was called. At that point we return EFAULT.
*/
static
void
copyfail(void)
{
longjmp(curthread->t_machdep.tm_copyjmp, 1);
}
/*
* Memory region check function. This checks to make sure the block of
* user memory provided (an address and a length) falls within the
* proper userspace region. If it does not, EFAULT is returned.
*
* stoplen is set to the actual maximum length that can be copied.
* This differs from len if and only if the region partially overlaps
* the kernel.
*
* Assumes userspace runs from 0 through USERSPACETOP-1.
*/
static
int
copycheck(const_userptr_t userptr, size_t len, size_t *stoplen)
{
vaddr_t bot, top;
*stoplen = len;
bot = (vaddr_t) userptr;
top = bot+len-1;
if (top < bot) {
/* addresses wrapped around */
return EFAULT;
}
if (bot >= USERSPACETOP) {
/* region is within the kernel */
return EFAULT;
}
if (top >= USERSPACETOP) {
/* region overlaps the kernel. adjust the max length. */
*stoplen = USERSPACETOP - bot;
}
return 0;
}
/*
* copyin
*
* Copy a block of memory of length LEN from user-level address USERSRC
* to kernel address DEST. We can use memcpy because it's protected by
* the tm_badfaultfunc/copyfail logic.
*/
int
copyin(const_userptr_t usersrc, void *dest, size_t len)
{
int result;
size_t stoplen;
result = copycheck(usersrc, len, &stoplen);
if (result) {
return result;
}
if (stoplen != len) {
/* Single block, can't legally truncate it. */
return EFAULT;
}
curthread->t_machdep.tm_badfaultfunc = copyfail;
result = setjmp(curthread->t_machdep.tm_copyjmp);
if (result) {
curthread->t_machdep.tm_badfaultfunc = NULL;
return EFAULT;
}
memcpy(dest, (const void *)usersrc, len);
curthread->t_machdep.tm_badfaultfunc = NULL;
return 0;
}
/*
* copyout
*
* Copy a block of memory of length LEN from kernel address SRC to
* user-level address USERDEST. We can use memcpy because it's
* protected by the tm_badfaultfunc/copyfail logic.
*/
int
copyout(const void *src, userptr_t userdest, size_t len)
{
int result;
size_t stoplen;
result = copycheck(userdest, len, &stoplen);
if (result) {
return result;
}
if (stoplen != len) {
/* Single block, can't legally truncate it. */
return EFAULT;
}
curthread->t_machdep.tm_badfaultfunc = copyfail;
result = setjmp(curthread->t_machdep.tm_copyjmp);
if (result) {
curthread->t_machdep.tm_badfaultfunc = NULL;
return EFAULT;
}
memcpy((void *)userdest, src, len);
curthread->t_machdep.tm_badfaultfunc = NULL;
return 0;
}
/*
* Common string copying function that behaves the way that's desired
* for copyinstr and copyoutstr.
*
* Copies a null-terminated string of maximum length MAXLEN from SRC
* to DEST. If GOTLEN is not null, store the actual length found
* there. Both lengths include the null-terminator. If the string
* exceeds the available length, the call fails and returns
* ENAMETOOLONG.
*
* STOPLEN is like MAXLEN but is assumed to have come from copycheck.
* If we hit MAXLEN it's because the string is too long to fit; if we
* hit STOPLEN it's because the string has run into the end of
* userspace. Thus in the latter case we return EFAULT, not
* ENAMETOOLONG.
*/
static
int
copystr(char *dest, const char *src, size_t maxlen, size_t stoplen,
size_t *gotlen)
{
size_t i;
for (i=0; i<maxlen && i<stoplen; i++) {
dest[i] = src[i];
if (src[i] == 0) {
if (gotlen != NULL) {
*gotlen = i+1;
}
return 0;
}
}
if (stoplen < maxlen) {
/* ran into user-kernel boundary */
return EFAULT;
}
/* otherwise just ran out of space */
return ENAMETOOLONG;
}
/*
* copyinstr
*
* Copy a string from user-level address USERSRC to kernel address
* DEST, as per copystr above. Uses the tm_badfaultfunc/copyfail
* logic to protect against invalid addresses supplied by a user
* process.
*/
int
copyinstr(const_userptr_t usersrc, char *dest, size_t len, size_t *actual)
{
int result;
size_t stoplen;
result = copycheck(usersrc, len, &stoplen);
if (result) {
return result;
}
curthread->t_machdep.tm_badfaultfunc = copyfail;
result = setjmp(curthread->t_machdep.tm_copyjmp);
if (result) {
curthread->t_machdep.tm_badfaultfunc = NULL;
return EFAULT;
}
result = copystr(dest, (const char *)usersrc, len, stoplen, actual);
curthread->t_machdep.tm_badfaultfunc = NULL;
return result;
}
/*
* copyoutstr
*
* Copy a string from kernel address SRC to user-level address
* USERDEST, as per copystr above. Uses the tm_badfaultfunc/copyfail
* logic to protect against invalid addresses supplied by a user
* process.
*/
int
copyoutstr(const char *src, userptr_t userdest, size_t len, size_t *actual)
{
int result;
size_t stoplen;
result = copycheck(userdest, len, &stoplen);
if (result) {
return result;
}
curthread->t_machdep.tm_badfaultfunc = copyfail;
result = setjmp(curthread->t_machdep.tm_copyjmp);
if (result) {
curthread->t_machdep.tm_badfaultfunc = NULL;
return EFAULT;
}
result = copystr((char *)userdest, src, len, stoplen, actual);
curthread->t_machdep.tm_badfaultfunc = NULL;
return result;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
/*
*用户/内核内存复制函数。
*
*这些设置是为了在无效的情况下防止致命的内核内存故障
*地址由用户级代码提供。这个代码本身就是
*机器独立;它使用依赖于机器的C setjmp/longjmp
*执行恢复的设施。
*
*但是,它假定内存子系统的某些方面可能不存在
*在所有平台上都是如此。
*
*(1)它假定用户内存映射到当前地址
*在内核中运行时的空间,只需
*以普通方式取消对指针的引用。(例如,
*使用特殊指令或通过特殊段寄存器。)
*
*(2)它假定内存的用户空间区域是连续的
*并从0扩展到某个虚拟地址USERSPACETOP,如果
*用户进程将内核地址传递给copycheck()中的逻辑
*我会抓住它的。
*
*(3)它假设从内核访问用户内存的行为
*与从用户空间访问用户内存的方式相同:for
*例如,处理器接受内存页上的只读位
*当处于内核模式时。
*
*(4)它假设如果一个正确的用户空间地址是有效的
*但是从内核中触摸不存在或根本无效,
*将发生正确的故障,并且VM系统将加载
*必要的页面等等。
*
*(5)它假设依赖于机器的陷阱逻辑提供和
*在thread_machdep结构中为tm_badfaultfunc字段命名。
*此功能的工作原理如下:如果发生其他致命故障
*在内核模式下,如果设置了tm_badfaultfunc,则在
*tm_badfaultfunc指向的函数。
*
*此代码通过设置tm_badfaultfunc然后复制内存来工作
*以普通的方式。如果满足这五个假设,
*这是许多普通CPU类型的情况,该代码应该
*功能正常。如果在某些情况下不满足假设
*平台(例如,某些旧的80386处理器
*假设3),此代码无法使用,并且cpu或平台-
*必须编写特定的代码。
*
*要使用此代码,除了tm_badfaultfunc
*thread_machdep结构应该包含一个名为
*“tm_copyjmp”。
*/
/*
*恢复功能。如果在copyin、copyout期间发生致命故障,
*copyinstr,或copyinstr,在此处继续执行。(这种行为是错误的
*由设置t_machdep.tm_badfaultfunc引起,并在中实现
*机器相关代码。)
*
*我们使用C标准函数longjmp()来传送调用
*堆栈到调用setjmp()的位置。在这一点上,我们返回EFAULT。
*/
静止的
无效的
复制失败(无效)
{
longjmp(curthread->t_machdep.tm_copyjmp,1);
}
/*
*内存区域检查功能。这将检查以确保
*提供的用户内存(地址和长度)在
*适当的用户空间区域。如果没有,则返回EFAULT。
*
*stoplen设置为可复制的实际最大长度。
*当且仅当区域部分重叠时,这与len不同
*内核。
*
*假定用户空间从0运行到USERSPACETOP-1。
*/
静止的
int
复制检查(常量用户ptr\u t用户ptr、大小长度、大小长度*停止长度)
{
vaddr_t机器人,顶部;
*stoplen=len;
bot=(vaddr_t)userptr;
top=bot+len-1;
如果(顶部<底部){
/*环绕的地址*/
返回默认值;
}
如果(bot>=USERSPACETOP){
/*区域位于内核中*/
返回默认值;
}
if(top>=USERSPACETOP){
/*区域与内核重叠。调整最大长度*/
*stoplen=USERSPACETOP-bot;
}
返回0;
}
/*
*抄袭
*
*从用户级地址USERSRC复制长度为LEN的内存块
*到内核地址DEST。我们可以使用memcpy,因为它受
*tm_badfaultfunc/copyfail逻辑。
*/
int
copyin(const_userptr_t usersrc,void*dest,size_t len)
{
int结果;
尺寸(t stoplen);;
结果=复制检查(usersrc、len和stoplen);
如果(结果){
返回结果;
}
if(stoplen!=len){
/*单个块,不能合法地截断它*/
返回默认值;
}
curthread->t_machdep.tm_badfaultfunc=copyfail;
结果=setjmp(curthread->t_machdep.tm_copyjmp);
如果(结果){
curthread->t_machdep.tm_badfaultfunc=NULL;
返回默认值;
}
memcpy(dest,(const void*)usersrc,len);
curthread->t_machdep.tm_badfaultfunc=NULL;
返回0;
}
/*
*抄写
*
*将长度为LEN的内存块从内核地址SRC复制到
*用户级地址USERDEST。我们可以使用memcpy,因为它是
*受tm_badfaultfunc/copyfail逻辑保护。
*/
int
复制输出(const void*src、userptr\t userdest、size\t len)
{
int结果;
尺寸(t stoplen);;
结果=复制检查(userdest、len和stoplen);
如果(结果){
返回结果;
}
if(stoplen!=len){
/*单个块,不能合法地截断它*/
返回默认值;
}
curthread->t_machdep.tm_badfaultfunc=copyfail;
结果=setjmp(curthread->t_machdep.tm_copyjmp);
如果(结果){
curthread->t_machdep.tm_badfaultfunc=NULL;
返回默认值;
}
memcpy((void*)userdest、src、len);
curthread->t_machdep.tm_badfaultfunc=NULL;