> 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/vm_map_stack.9.md).

# vm\_map\_stack.9

`vm_map_stack`, `vm_map_growstack` — 管理进程栈

## 名称

`vm_map_stack`, `vm_map_growstack`

## 概要

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

int
vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
    vm_prot_t prot, vm_prot_t max, int cow)

int
vm_map_growstack(struct proc *p, vm_offset_t addr)
```

## 描述

`vm_map_stack` 函数为新进程映像映射进程栈。栈映射到 `map` 中的 `addrbos` 处，最大大小为 `max_ssize`。传入 `cow` 的写时复制标志也应用到新映射。保护位由 `prot` 和 `max` 提供。

通常由 [execve(2)](https://github.com/FreeBSD-Ask/freebsd-man-sc/tree/main/man2/execve.2.md) 调用。

`vm_map_growstack` 函数负责将进程 `p` 的栈增长到所需地址 `addr`，类似于传统的 [sbrk(2)](https://github.com/FreeBSD-Ask/freebsd-man-sc/tree/main/man2/sbrk.2.md) 调用。

## 实现说明

`vm_map_stack` 函数调用 [vm\_map\_insert(9)](/man/man9/vm_map_insert.9.md) 来创建其映射。

`vm_map_stack` 和 `vm_map_growstack` 函数在调用期间获取进程 `p` 上的进程锁。

## 返回值

`vm_map_stack` 函数在映射成功分配时返回 `KERN_SUCCESS`。

否则，如果映射栈会超过进程的 VMEM 资源限制，或指定的栈底地址超出映射范围，或该地址已有映射，或 `max_ssize` 无法在当前映射中容纳，则返回 `KERN_NO_SPACE`。

此函数其他可能的返回值记录在 [vm\_map\_insert(9)](/man/man9/vm_map_insert.9.md) 中。

`vm_map_growstack` 函数在 `addr` 已被映射或栈成功增长时返回 `KERN_SUCCESS`。

如果 `addr` 在栈范围之外，它也返回 `KERN_SUCCESS`；这样做是为了保持与先前位于 `vm_machdep.c` 文件中的已弃用 `grow` 函数的兼容性。

## 参见

[vm\_map(9)](/man/man9/vm_map.9.md), [vm\_map\_insert(9)](/man/man9/vm_map_insert.9.md)

## 作者

本手册页由 Bruce M Simpson <bms@spc.org> 编写。


---

# 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/vm_map_stack.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.
