os161中的userptr_t类型有什么用途?

如何解决os161中的userptr_t类型有什么用途?

我正在尝试完成操作系统课程Here的作业。

我对作业有疑问:

userptr_t的用途是什么?

当我在源代码中搜索userptr_tHere时,发现:

/*
 * 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;

我无法完全理解它的用途,谁能解释这种类型的目的是什么?

例如,在函数copyinout.ccopyincopyoutcopyinstr和其他函数的文件copyoutstr中使用它

#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,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 *actual)
{
    int result;
    size_t stoplen;

    result = copycheck(usersrc,&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,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,size_t *actual)
{
    int result;
    size_t stoplen;

    result = copycheck(userdest,&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,actual);

    curthread->t_machdep.tm_badfaultfunc = NULL;
    return result;
}

解决方法

这看起来像一个强大的typedef,即,一个typedef旨在通过避免包装数据的意外使用/转换来提高类型安全性。

在您的上下文中,最有可能打算将内核指针与用户空间指针(通常通过MMU映射)区分开来。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-