> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man9/vslock.9.md).

# vslock.9

`vslock`, `vsunlock` — 将用户空间地址锁定/解锁在内存中

## 名称

`vslock`, `vsunlock`

## 概要

```c
#include <sys/param.h>
#include <sys/proc.h>
#include <vm/vm.h>
#include <vm/vm_extern.h>

int
vslock(void *addr, size_t len)

void
vsunlock(void *addr, size_t len)
```

## 描述

`vslock` 和 `vsunlock` 函数分别将当前运行进程拥有的地址范围锁定和解锁到内存中。实际锁定的内存量是机器页大小的倍数。起始页号通过将 `addr` 截断到最近的前一个页边界计算，并将 `addr` + `len` 向上舍入到下一个页边界。用于此操作的进程上下文取自全局变量 `curproc`。

## 返回值

`vslock` 函数成功时返回 0，否则返回以下列出的错误之一。

## 错误

`vslock` 函数在以下情况失败：

**`[EINVAL]`** `addr` 和 `len` 参数指定的内存范围环绕机器地址空间的末尾。

**`[ENOMEM]`** 指定地址范围的大小超过系统对锁定内存的限制。

**`[EFAULT]`** 指示地址范围的某些部分未分配。在页面错误/映射时出错。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man9/vslock.9.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
