## Summary

Create a resource object.

## Declaration

    #include <zircon/syscalls.h>

    zx_status_t zx_resource_create(zx_handle_t parent_rsrc,
                                   uint32_t options,
                                   uint64_t base,
                                   size_t size,
                                   const char* name,
                                   size_t name_size,
                                   zx_handle_t* resource_out);

## Description

`zx_resource_create()` creates a resource object for use with other DDK
syscalls. Resources are typically handed out to bus drivers and rarely need to
be interacted with directly by drivers using driver protocols. Resource objects
grant access to an address space range starting at *base* up to but not
including *base* + *size* . If *kind* is `ZX_RSRC_KIND_MMIO`, then *base*
and *size* must be multiples of `zx_system_get_page_size()`. A special value for *kind* exists:
`ZX_RSRC_KIND_SYSTEM`. The system resource is a ranged resource which is the parent
of a number of resources, all of *size* 1, which are used as privileged checks
for syscalls.

*parent_rsrc* must be a handle to a resource that matches the requested *kind*
and contains \[*base* , *base* +*size*\] in its range.

*options* must specify which kind of resource to create and may contain optional
flags. Valid kinds of resources are `ZX_RSRC_KIND_MMIO`, `ZX_RSRC_KIND_IRQ`,
`ZX_RSRC_KIND_IOPORT` (x86 only), `ZX_RSRC_KIND_SMC` (ARM only), and
`ZX_RSRC_KIND_SYSTEM`.
Valid bases for `ZX_RSRC_KIND_SYSTEM` are `ZX_RSRC_SYSTEM_HYPERVISOR_BASE`,
`ZX_RSRC_SYSTEM_VMEX_BASE`, `ZX_RSRC_SYSTEM_DEBUG_BASE`,
`ZX_RSRC_SYSTEM_INFO_BASE`, `ZX_RSRC_SYSTEM_CPU_BASE`,
`ZX_RSRC_SYSTEM_POWER_BASE`, `ZX_RSRC_SYSTEM_ENERGY_INFO_BASE`,
`ZX_RSRC_SYSTEM_IOMMU_BASE`,
`ZX_RSRC_SYSTEM_PROFILE_BASE`, / `ZX_RSRC_SYSTEM_MSI_BASE`,
`ZX_RSRC_SYSTEM_DEBUGLOG_BASE` and `ZX_RSRC_SYSTEM_STALL_BASE`. These all have
*size* 1.

The `ZX_RSRC_FLAG_EXCLUSIVE` flag is optional. If `ZX_RSRC_FLAG_EXCLUSIVE` is
provided then the syscall will attempt to exclusively reserve the requested
address space region, preventing other resources creation from overlapping with
it as long as it exists.

*name* and *name_size* are optional and truncated to `ZX_MAX_NAME_LENGTH` - 1.
This name is provided for debugging / tool use only and is not used by the
kernel.

On success, a valid resource handle is returned in *resource_out*.

The following rights will be set on the handle by default:

- `ZX_RIGHT_DUPLICATE` - The handle may be duplicated.

- `ZX_RIGHT_TRANSFER` - The handle may be transferred to another process.

- `ZX_RIGHT_WRITE` - Checked by `zx_resource_create()` itself.

- `ZX_RIGHT_INSPECT` - May inspect the object with [`zx_object_get_info()`](https://fuchsia.dev/reference/syscalls/object_get_info).

- `ZX_RIGHT_GET_PROPERTY` - May get its properties using [`zx_object_get_property()`](https://fuchsia.dev/reference/syscalls/object_get_property).

- `ZX_RIGHT_SET_PROPERTY` - May set its properties using [`zx_object_set_property()`](https://fuchsia.dev/reference/syscalls/object_set_property).

## Return value

`zx_resource_create()` returns `ZX_OK` on success. In the event of failure, a
negative error value is returned.

## Rights

*parent_rsrc* must be of type `ZX_OBJ_TYPE_RESOURCE` and have `ZX_RIGHT_WRITE`.

## Errors

`ZX_ERR_BAD_HANDLE` the *parent_rsrc* handle is invalid.

`ZX_ERR_WRONG_TYPE` the *parent_rsrc* handle is not a resource handle.

`ZX_ERR_ACCESS_DENIED` The *parent_rsrc* handle is not a resource of *kind*.

`ZX_ERR_INVALID_ARGS` *options* contains an invalid kind or flag combination,
*name* is an invalid pointer, or *options* has `ZX_RSRC_KIND_MMIO` and *base*
or *size* is not a multiple of `zx_system_get_page_size()`.

`ZX_ERR_NOT_FOUND` A suitable resource allocation was not found. This happens
if the address range is not valid, if the range is already owned by an exclusive
resource, or if a regular resource exists and the caller is trying to create
an exclusive resource in the same range.

`ZX_ERR_NO_MEMORY` Failure due to lack of memory. There is no good way for
userspace to handle this (unlikely) error. In a future build this error will no
longer occur.

## See also

- [`zx_handle_close()`](https://fuchsia.dev/reference/syscalls/handle_close)
- [`zx_interrupt_create()`](https://fuchsia.dev/reference/syscalls/interrupt_create)
- [`zx_ioports_request()`](https://fuchsia.dev/reference/syscalls/ioports_request)
- [`zx_vmo_create_physical()`](https://fuchsia.dev/reference/syscalls/vmo_create_physical)