mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
Merge pull request #421 from QuixiAI/feat/nvkms-ownership-and-lifecycle-ffi
feat(nvkms): move ownership table & lifecycle validators into Rust FFI (9/66 PORTED)
This commit is contained in:
@ -9,10 +9,10 @@ Generated by `python3 tools/build_parity_ledger.py` against stock 580.178.04 ker
|
||||
| `rm_controls_userspace` | 295 | 0 | 0 | 14 |
|
||||
| `uvm_ioctls` | 49 | 0 | 0 | 1 |
|
||||
| `drm_ioctls` | 14 | 10 | 0 | 0 |
|
||||
| `nvkms_ioctls` | 4 | 62 | 0 | 0 |
|
||||
| `nvkms_ioctls` | 9 | 57 | 0 | 0 |
|
||||
| `procfs_and_caps` | 9 | 0 | 0 | 0 |
|
||||
| `mmio_bus_loss_guards` | 32 | 0 | 0 | 0 |
|
||||
| **TOTAL** | **2544** | **72** | **0** | **19** |
|
||||
| **TOTAL** | **2549** | **67** | **0** | **19** |
|
||||
|
||||
## Remaining `MISSING` / `PARTIAL` Entries
|
||||
|
||||
@ -26,7 +26,6 @@ Generated by `python3 tools/build_parity_ledger.py` against stock 580.178.04 ker
|
||||
- **[PARTIAL]** `drm_ioctls` `nv_drm_semsurf_fence_create_ioctl` — UAPI struct + ioctl NVIDIA_SEMSURF_FENCE_CREATE wired in nvidia_rs_drm.c (returns -EOPNOTSUPP until GEM/fence backend)
|
||||
- **[PARTIAL]** `drm_ioctls` `nv_drm_semsurf_fence_ctx_create_ioctl` — UAPI struct + ioctl NVIDIA_SEMSURF_FENCE_CTX_CREATE wired in nvidia_rs_drm.c (returns -EOPNOTSUPP until GEM/fence backend)
|
||||
- **[PARTIAL]** `drm_ioctls` `nv_drm_semsurf_fence_wait_ioctl` — UAPI struct + ioctl NVIDIA_SEMSURF_FENCE_WAIT wired in nvidia_rs_drm.c (returns -EOPNOTSUPP until GEM/fence backend)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `1:NVKMS_IOCTL_FREE_DEVICE` — cmd=1 exact 580.178.04 paramSize=8 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `2:NVKMS_IOCTL_QUERY_DISP` — cmd=2 exact 580.178.04 paramSize=172 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `3:NVKMS_IOCTL_QUERY_CONNECTOR_STATIC_DATA` — cmd=3 exact 580.178.04 paramSize=44 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `4:NVKMS_IOCTL_QUERY_CONNECTOR_DYNAMIC_DATA` — cmd=4 exact 580.178.04 paramSize=20 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
@ -57,20 +56,16 @@ Generated by `python3 tools/build_parity_ledger.py` against stock 580.178.04 ker
|
||||
- **[PARTIAL]** `nvkms_ioctls` `29:NVKMS_IOCTL_SET_FRAMELOCK_ATTRIBUTE` — cmd=29 exact 580.178.04 paramSize=24 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `30:NVKMS_IOCTL_GET_FRAMELOCK_ATTRIBUTE` — cmd=30 exact 580.178.04 paramSize=16 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `31:NVKMS_IOCTL_GET_FRAMELOCK_ATTRIBUTE_VALID_VALUES` — cmd=31 exact 580.178.04 paramSize=32 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `32:NVKMS_IOCTL_GET_NEXT_EVENT` — cmd=32 exact 580.178.04 paramSize=48 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `34:NVKMS_IOCTL_CLEAR_UNICAST_EVENT` — cmd=34 exact 580.178.04 paramSize=8 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `35:NVKMS_IOCTL_GET_3DVISION_DONGLE_PARAM_BYTES` — cmd=35 exact 580.178.04 paramSize=32 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `36:NVKMS_IOCTL_SET_3DVISION_AEGIS_PARAMS` — cmd=36 exact 580.178.04 paramSize=24 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `37:NVKMS_IOCTL_SET_LAYER_POSITION` — cmd=37 exact 580.178.04 paramSize=1196 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `38:NVKMS_IOCTL_GRAB_OWNERSHIP` — cmd=38 exact 580.178.04 paramSize=8 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `39:NVKMS_IOCTL_RELEASE_OWNERSHIP` — cmd=39 exact 580.178.04 paramSize=8 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `40:NVKMS_IOCTL_GRANT_PERMISSIONS` — cmd=40 exact 580.178.04 paramSize=144 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `41:NVKMS_IOCTL_ACQUIRE_PERMISSIONS` — cmd=41 exact 580.178.04 paramSize=140 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `42:NVKMS_IOCTL_REVOKE_PERMISSIONS` — cmd=42 exact 580.178.04 paramSize=144 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `43:NVKMS_IOCTL_QUERY_DPY_CRC32` — cmd=43 exact 580.178.04 paramSize=36 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `44:NVKMS_IOCTL_REGISTER_DEFERRED_REQUEST_FIFO` — cmd=44 exact 580.178.04 paramSize=12 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `45:NVKMS_IOCTL_UNREGISTER_DEFERRED_REQUEST_FIFO` — cmd=45 exact 580.178.04 paramSize=12 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `47:NVKMS_IOCTL_FREE_SWAP_GROUP` — cmd=47 exact 580.178.04 paramSize=12 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `48:NVKMS_IOCTL_JOIN_SWAP_GROUP` — cmd=48 exact 580.178.04 paramSize=2568 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `49:NVKMS_IOCTL_LEAVE_SWAP_GROUP` — cmd=49 exact 580.178.04 paramSize=1032 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
- **[PARTIAL]** `nvkms_ioctls` `50:NVKMS_IOCTL_SET_SWAP_GROUP_CLIP_LIST` — cmd=50 exact 580.178.04 paramSize=32 + deviceHandle validated in nvidia_rs_modeset.c (display/surface backend or per-cmd test pending)
|
||||
|
||||
@ -21,6 +21,15 @@ extern int nv_rs_nvkms_validate_alloc_device(u64 address, u32 size, u32 rm_devic
|
||||
u32 mig_device);
|
||||
extern int nv_rs_nvkms_validate_event_interest(u64 address, u32 size, u32 mask);
|
||||
extern int nv_rs_nvkms_validate_alloc_swap_group(u64 address, u32 size, const u32 *head_masks);
|
||||
extern int nv_rs_nvkms_validate_free_device(u64 address, u32 size, u32 device_handle);
|
||||
extern int nv_rs_nvkms_validate_get_next_event(u64 address, u32 size);
|
||||
extern int nv_rs_nvkms_validate_free_swap_group(u64 address, u32 size, u32 device_handle,
|
||||
u32 swap_group_handle);
|
||||
extern int nv_rs_nvkms_ownership_grab(u32 rm_device_id, u64 file_id, u32 device_handle);
|
||||
extern int nv_rs_nvkms_ownership_release(u32 rm_device_id, u64 file_id, u32 device_handle);
|
||||
extern u8 nv_rs_nvkms_ownership_is_owner(u32 rm_device_id, u64 file_id, u32 device_handle);
|
||||
extern void nv_rs_nvkms_ownership_release_device(u64 file_id, u32 device_handle);
|
||||
extern void nv_rs_nvkms_ownership_release_file(u64 file_id);
|
||||
extern int nv_rs_nvkms_validate_fbcon_disabled_userspace(u64 address, u32 size, u8 device_exists);
|
||||
|
||||
/* Exact NvKmsIoctlParams UAPI from 580.178.04 nvkms-ioctl.h. */
|
||||
@ -120,19 +129,11 @@ struct nv_rs_modeset_file {
|
||||
struct nv_rs_kms_swap_group_slot swap_groups[NV_RS_KMS_MAX_SWAP_GROUPS_PER_FILE];
|
||||
};
|
||||
|
||||
struct nv_rs_kms_owner_entry {
|
||||
bool in_use;
|
||||
struct nv_rs_modeset_file *owner_file;
|
||||
u32 owner_device_handle;
|
||||
};
|
||||
|
||||
/*
|
||||
* Module-wide exclusive display-ownership table indexed directly by validated
|
||||
* rm_device_id (< NV_RS_KMS_MAX_RM_DEVICES, matching stock NV_MAX_DEVICES).
|
||||
* Module-wide exclusive display-ownership lock guarding Rust's OwnershipTable.
|
||||
* Lock ordering: always acquire nv_rs_modeset_owner_lock before f->lock.
|
||||
*/
|
||||
static DEFINE_MUTEX(nv_rs_modeset_owner_lock);
|
||||
static struct nv_rs_kms_owner_entry nv_rs_modeset_owners[NV_RS_KMS_MAX_RM_DEVICES];
|
||||
|
||||
static const struct file_operations nv_rs_modeset_fops;
|
||||
|
||||
@ -140,29 +141,22 @@ static void nv_rs_modeset_release_owner_locked(struct nv_rs_modeset_file *f,
|
||||
u32 device_handle,
|
||||
bool all_for_file)
|
||||
{
|
||||
u32 i;
|
||||
u64 file_id = (u64)(uintptr_t)f;
|
||||
|
||||
for (i = 0; i < NV_RS_KMS_MAX_RM_DEVICES; i++) {
|
||||
if (!nv_rs_modeset_owners[i].in_use)
|
||||
continue;
|
||||
if (nv_rs_modeset_owners[i].owner_file != f)
|
||||
continue;
|
||||
if (all_for_file || nv_rs_modeset_owners[i].owner_device_handle == device_handle) {
|
||||
nv_rs_modeset_owners[i].in_use = false;
|
||||
nv_rs_modeset_owners[i].owner_file = NULL;
|
||||
nv_rs_modeset_owners[i].owner_device_handle = 0;
|
||||
}
|
||||
}
|
||||
if (all_for_file)
|
||||
nv_rs_nvkms_ownership_release_file(file_id);
|
||||
else
|
||||
nv_rs_nvkms_ownership_release_device(file_id, device_handle);
|
||||
}
|
||||
|
||||
static bool nv_rs_modeset_is_owner_locked(const struct nv_rs_modeset_file *f,
|
||||
const struct nv_rs_kms_device_slot *dev)
|
||||
{
|
||||
if (!dev || dev->rm_device_id >= NV_RS_KMS_MAX_RM_DEVICES)
|
||||
if (!dev)
|
||||
return false;
|
||||
return nv_rs_modeset_owners[dev->rm_device_id].in_use &&
|
||||
nv_rs_modeset_owners[dev->rm_device_id].owner_file == f &&
|
||||
nv_rs_modeset_owners[dev->rm_device_id].owner_device_handle == dev->handle;
|
||||
return nv_rs_nvkms_ownership_is_owner(dev->rm_device_id,
|
||||
(u64)(uintptr_t)f,
|
||||
dev->handle) != 0;
|
||||
}
|
||||
|
||||
static void nv_rs_modeset_clear_swap_groups_locked(struct nv_rs_modeset_file *f,
|
||||
@ -310,8 +304,11 @@ static long nv_rs_modeset_free_device(struct nv_rs_modeset_file *f,
|
||||
return -EINVAL;
|
||||
if (copy_from_user(&req, (const void __user *)(uintptr_t)p->address, sizeof(req)))
|
||||
return -EFAULT;
|
||||
if (req.device_handle == 0)
|
||||
return -EINVAL;
|
||||
{
|
||||
int rc = nv_rs_nvkms_validate_free_device(p->address, p->size, req.device_handle);
|
||||
if (rc)
|
||||
return rc;
|
||||
}
|
||||
|
||||
mutex_lock(&nv_rs_modeset_owner_lock);
|
||||
mutex_lock(&f->lock);
|
||||
@ -377,9 +374,10 @@ static long nv_rs_modeset_register_surface(struct nv_rs_modeset_file *f,
|
||||
static long nv_rs_modeset_get_next_event(const struct nv_rs_kms_ioctl_params *p)
|
||||
{
|
||||
u8 zeros[NV_RS_KMS_GET_NEXT_EVENT_REPLY_SIZE];
|
||||
int rc = nv_rs_nvkms_validate_get_next_event(p->address, p->size);
|
||||
|
||||
if (!p->address || p->size != NV_RS_KMS_GET_NEXT_EVENT_SIZE)
|
||||
return -EINVAL;
|
||||
if (rc)
|
||||
return rc;
|
||||
memset(zeros, 0, sizeof(zeros));
|
||||
/* valid = 0 (FALSE): no pending NVKMS events. */
|
||||
if (copy_to_user((u8 __user *)(uintptr_t)p->address +
|
||||
@ -464,7 +462,7 @@ static long nv_rs_modeset_ownership(struct nv_rs_modeset_file *f,
|
||||
u32 device_handle = 0;
|
||||
u32 zero = 0;
|
||||
struct nv_rs_kms_device_slot *dev;
|
||||
struct nv_rs_kms_owner_entry *slot;
|
||||
int rc;
|
||||
|
||||
if (!p->address || p->size != NV_RS_KMS_SIMPLE_8B_SIZE)
|
||||
return -EINVAL;
|
||||
@ -475,45 +473,28 @@ static long nv_rs_modeset_ownership(struct nv_rs_modeset_file *f,
|
||||
mutex_lock(&nv_rs_modeset_owner_lock);
|
||||
mutex_lock(&f->lock);
|
||||
dev = nv_rs_modeset_find_device_locked(f, device_handle);
|
||||
if (!dev || dev->rm_device_id >= NV_RS_KMS_MAX_RM_DEVICES) {
|
||||
if (!dev) {
|
||||
mutex_unlock(&f->lock);
|
||||
mutex_unlock(&nv_rs_modeset_owner_lock);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
slot = &nv_rs_modeset_owners[dev->rm_device_id];
|
||||
if (grab) {
|
||||
if (slot->in_use) {
|
||||
if (slot->owner_file != f || slot->owner_device_handle != device_handle) {
|
||||
mutex_unlock(&f->lock);
|
||||
mutex_unlock(&nv_rs_modeset_owner_lock);
|
||||
return -EBUSY;
|
||||
}
|
||||
/* Repeated grab by the current owner succeeds idempotently. */
|
||||
rc = nv_rs_nvkms_ownership_grab(dev->rm_device_id, (u64)(uintptr_t)f, device_handle);
|
||||
if (rc == 0)
|
||||
dev->has_ownership = true;
|
||||
} else {
|
||||
slot->in_use = true;
|
||||
slot->owner_file = f;
|
||||
slot->owner_device_handle = device_handle;
|
||||
dev->has_ownership = true;
|
||||
}
|
||||
} else {
|
||||
if (!slot->in_use ||
|
||||
slot->owner_file != f ||
|
||||
slot->owner_device_handle != device_handle) {
|
||||
mutex_unlock(&f->lock);
|
||||
mutex_unlock(&nv_rs_modeset_owner_lock);
|
||||
return -EPERM;
|
||||
rc = nv_rs_nvkms_ownership_release(dev->rm_device_id, (u64)(uintptr_t)f, device_handle);
|
||||
if (rc == 0) {
|
||||
/* Stock ownership release frees swap groups allocated on this device. */
|
||||
nv_rs_modeset_clear_swap_groups_locked(f, device_handle);
|
||||
dev->has_ownership = false;
|
||||
}
|
||||
slot->in_use = false;
|
||||
slot->owner_file = NULL;
|
||||
slot->owner_device_handle = 0;
|
||||
/* Stock ownership release frees swap groups allocated on this device. */
|
||||
nv_rs_modeset_clear_swap_groups_locked(f, device_handle);
|
||||
dev->has_ownership = false;
|
||||
}
|
||||
mutex_unlock(&f->lock);
|
||||
mutex_unlock(&nv_rs_modeset_owner_lock);
|
||||
if (rc)
|
||||
return rc;
|
||||
|
||||
if (copy_to_user((u8 __user *)(uintptr_t)p->address + 4u, &zero, sizeof(zero)))
|
||||
return -EFAULT;
|
||||
@ -598,6 +579,11 @@ static long nv_rs_modeset_free_swap_group(struct nv_rs_modeset_file *f,
|
||||
return -EINVAL;
|
||||
if (copy_from_user(req, (const void __user *)(uintptr_t)p->address, sizeof(req)))
|
||||
return -EFAULT;
|
||||
{
|
||||
int rc = nv_rs_nvkms_validate_free_swap_group(p->address, p->size, req[0], req[1]);
|
||||
if (rc)
|
||||
return rc;
|
||||
}
|
||||
|
||||
mutex_lock(&nv_rs_modeset_owner_lock);
|
||||
mutex_lock(&f->lock);
|
||||
|
||||
@ -22,6 +22,15 @@ extern int nv_rs_nvkms_validate_alloc_device(u64 address, u32 size, u32 rm_devic
|
||||
u32 mig_device);
|
||||
extern int nv_rs_nvkms_validate_event_interest(u64 address, u32 size, u32 mask);
|
||||
extern int nv_rs_nvkms_validate_alloc_swap_group(u64 address, u32 size, const u32 *head_masks);
|
||||
extern int nv_rs_nvkms_validate_free_device(u64 address, u32 size, u32 device_handle);
|
||||
extern int nv_rs_nvkms_validate_get_next_event(u64 address, u32 size);
|
||||
extern int nv_rs_nvkms_validate_free_swap_group(u64 address, u32 size, u32 device_handle,
|
||||
u32 swap_group_handle);
|
||||
extern int nv_rs_nvkms_ownership_grab(u32 rm_device_id, u64 file_id, u32 device_handle);
|
||||
extern int nv_rs_nvkms_ownership_release(u32 rm_device_id, u64 file_id, u32 device_handle);
|
||||
extern u8 nv_rs_nvkms_ownership_is_owner(u32 rm_device_id, u64 file_id, u32 device_handle);
|
||||
extern void nv_rs_nvkms_ownership_release_device(u64 file_id, u32 device_handle);
|
||||
extern void nv_rs_nvkms_ownership_release_file(u64 file_id);
|
||||
extern int nv_rs_nvkms_validate_fbcon_disabled_userspace(u64 address, u32 size, u8 device_exists);
|
||||
extern int nv_rs_drm_validate_gem_alloc_nvkms(u8 modeset_enabled, u16 pad0, u32 pad1,
|
||||
u64 memory_size, u64 *out_aligned_size);
|
||||
@ -40,6 +49,14 @@ EXPORT_SYMBOL(nv_rs_nvkms_param_size_580);
|
||||
EXPORT_SYMBOL(nv_rs_nvkms_validate_alloc_device);
|
||||
EXPORT_SYMBOL(nv_rs_nvkms_validate_event_interest);
|
||||
EXPORT_SYMBOL(nv_rs_nvkms_validate_alloc_swap_group);
|
||||
EXPORT_SYMBOL(nv_rs_nvkms_validate_free_device);
|
||||
EXPORT_SYMBOL(nv_rs_nvkms_validate_get_next_event);
|
||||
EXPORT_SYMBOL(nv_rs_nvkms_validate_free_swap_group);
|
||||
EXPORT_SYMBOL(nv_rs_nvkms_ownership_grab);
|
||||
EXPORT_SYMBOL(nv_rs_nvkms_ownership_release);
|
||||
EXPORT_SYMBOL(nv_rs_nvkms_ownership_is_owner);
|
||||
EXPORT_SYMBOL(nv_rs_nvkms_ownership_release_device);
|
||||
EXPORT_SYMBOL(nv_rs_nvkms_ownership_release_file);
|
||||
EXPORT_SYMBOL(nv_rs_nvkms_validate_fbcon_disabled_userspace);
|
||||
EXPORT_SYMBOL(nv_rs_drm_validate_gem_alloc_nvkms);
|
||||
EXPORT_SYMBOL(nv_rs_drm_validate_gem_import_userspace);
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -581,6 +581,105 @@ pub unsafe extern "C" fn nv_rs_nvkms_validate_alloc_swap_group(
|
||||
}
|
||||
}
|
||||
|
||||
/// Global NVKMS exclusive display ownership table; caller (`nvidia_rs_modeset.c`)
|
||||
/// serializes all mutations under `nv_rs_modeset_owner_lock`.
|
||||
static mut NVKMS_OWNERSHIP: nv_rs_core::nvkms::OwnershipTable =
|
||||
nv_rs_core::nvkms::OwnershipTable::new();
|
||||
|
||||
/// FFI entry point for `nvidia_rs_modeset.ko`: validates `NVKMS_IOCTL_FREE_DEVICE`.
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn nv_rs_nvkms_validate_free_device(
|
||||
address: u64,
|
||||
size: u32,
|
||||
device_handle: u32,
|
||||
) -> i32 {
|
||||
match nv_rs_core::nvkms::validate_free_device(address, size, device_handle) {
|
||||
Ok(()) => 0,
|
||||
Err(e) => e,
|
||||
}
|
||||
}
|
||||
|
||||
/// FFI entry point for `nvidia_rs_modeset.ko`: validates `NVKMS_IOCTL_GET_NEXT_EVENT`.
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn nv_rs_nvkms_validate_get_next_event(address: u64, size: u32) -> i32 {
|
||||
match nv_rs_core::nvkms::validate_get_next_event(address, size) {
|
||||
Ok(()) => 0,
|
||||
Err(e) => e,
|
||||
}
|
||||
}
|
||||
|
||||
/// FFI entry point for `nvidia_rs_modeset.ko`: validates `NVKMS_IOCTL_FREE_SWAP_GROUP`.
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn nv_rs_nvkms_validate_free_swap_group(
|
||||
address: u64,
|
||||
size: u32,
|
||||
device_handle: u32,
|
||||
swap_group_handle: u32,
|
||||
) -> i32 {
|
||||
match nv_rs_core::nvkms::validate_free_swap_group(
|
||||
address,
|
||||
size,
|
||||
device_handle,
|
||||
swap_group_handle,
|
||||
) {
|
||||
Ok(()) => 0,
|
||||
Err(e) => e,
|
||||
}
|
||||
}
|
||||
|
||||
/// FFI entry point for `nvidia_rs_modeset.ko`: grabs exclusive display ownership.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn nv_rs_nvkms_ownership_grab(
|
||||
rm_device_id: u32,
|
||||
file_id: u64,
|
||||
device_handle: u32,
|
||||
) -> i32 {
|
||||
let tbl = unsafe { &mut *core::ptr::addr_of_mut!(NVKMS_OWNERSHIP) };
|
||||
match tbl.grab(rm_device_id, file_id, device_handle) {
|
||||
Ok(()) => 0,
|
||||
Err(e) => e,
|
||||
}
|
||||
}
|
||||
|
||||
/// FFI entry point for `nvidia_rs_modeset.ko`: releases exclusive display ownership.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn nv_rs_nvkms_ownership_release(
|
||||
rm_device_id: u32,
|
||||
file_id: u64,
|
||||
device_handle: u32,
|
||||
) -> i32 {
|
||||
let tbl = unsafe { &mut *core::ptr::addr_of_mut!(NVKMS_OWNERSHIP) };
|
||||
match tbl.release(rm_device_id, file_id, device_handle) {
|
||||
Ok(()) => 0,
|
||||
Err(e) => e,
|
||||
}
|
||||
}
|
||||
|
||||
/// FFI entry point for `nvidia_rs_modeset.ko`: checks exclusive display ownership.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn nv_rs_nvkms_ownership_is_owner(
|
||||
rm_device_id: u32,
|
||||
file_id: u64,
|
||||
device_handle: u32,
|
||||
) -> u8 {
|
||||
let tbl = unsafe { &*core::ptr::addr_of!(NVKMS_OWNERSHIP) };
|
||||
u8::from(tbl.is_owner(rm_device_id, file_id, device_handle))
|
||||
}
|
||||
|
||||
/// FFI entry point for `nvidia_rs_modeset.ko`: clears ownership for one device handle.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn nv_rs_nvkms_ownership_release_device(file_id: u64, device_handle: u32) {
|
||||
let tbl = unsafe { &mut *core::ptr::addr_of_mut!(NVKMS_OWNERSHIP) };
|
||||
tbl.release_device(file_id, device_handle);
|
||||
}
|
||||
|
||||
/// FFI entry point for `nvidia_rs_modeset.ko`: clears all ownership for a closed file.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn nv_rs_nvkms_ownership_release_file(file_id: u64) {
|
||||
let tbl = unsafe { &mut *core::ptr::addr_of_mut!(NVKMS_OWNERSHIP) };
|
||||
tbl.release_all_for_file(file_id);
|
||||
}
|
||||
|
||||
/// FFI entry point for `nvidia_rs_modeset.ko`: validates `NVKMS_IOCTL_FRAMEBUFFER_CONSOLE_DISABLED`.
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn nv_rs_nvkms_validate_fbcon_disabled_userspace(
|
||||
|
||||
@ -53,6 +53,35 @@ pub const fn validate_alloc_device(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Validates `NVKMS_IOCTL_FREE_DEVICE` (`cmd = 1`, 8 bytes) request parameters.
|
||||
pub const fn validate_free_device(address: u64, size: u32, device_handle: u32) -> Result<(), i32> {
|
||||
if address == 0 || size < 4 || size > 65536 || device_handle == 0 {
|
||||
return Err(EINVAL);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Validates `NVKMS_IOCTL_GET_NEXT_EVENT` (`cmd = 32`, 48 bytes) request parameters.
|
||||
pub const fn validate_get_next_event(address: u64, size: u32) -> Result<(), i32> {
|
||||
if address == 0 || size != 48 {
|
||||
return Err(EINVAL);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Validates `NVKMS_IOCTL_FREE_SWAP_GROUP` (`cmd = 47`, 12 bytes) request parameters.
|
||||
pub const fn validate_free_swap_group(
|
||||
address: u64,
|
||||
size: u32,
|
||||
device_handle: u32,
|
||||
swap_group_handle: u32,
|
||||
) -> Result<(), i32> {
|
||||
if address == 0 || size != 12 || device_handle == 0 || swap_group_handle == 0 {
|
||||
return Err(EINVAL);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Validates `NVKMS_IOCTL_DECLARE_EVENT_INTEREST` (`cmd = 33`) request parameters.
|
||||
pub const fn validate_event_interest(address: u64, size: u32, mask: u32) -> Result<(), i32> {
|
||||
if address == 0 || size != 8 {
|
||||
@ -174,6 +203,19 @@ impl OwnershipTable {
|
||||
slot.in_use && slot.owner_file_id == file_id && slot.owner_device_handle == device_handle
|
||||
}
|
||||
|
||||
pub fn release_device(&mut self, file_id: u64, device_handle: u32) {
|
||||
for slot in &mut self.entries {
|
||||
if slot.in_use
|
||||
&& slot.owner_file_id == file_id
|
||||
&& slot.owner_device_handle == device_handle
|
||||
{
|
||||
slot.in_use = false;
|
||||
slot.owner_file_id = 0;
|
||||
slot.owner_device_handle = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn release_all_for_file(&mut self, file_id: u64) {
|
||||
for slot in &mut self.entries {
|
||||
if slot.in_use && slot.owner_file_id == file_id {
|
||||
@ -249,25 +291,53 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exclusive_ownership_table_matches_stock_semantics() {
|
||||
fn test_free_device() {
|
||||
assert_eq!(validate_free_device(0, 8, 1), Err(EINVAL));
|
||||
assert_eq!(validate_free_device(0x1000, 2, 1), Err(EINVAL));
|
||||
assert_eq!(validate_free_device(0x1000, 8, 0), Err(EINVAL));
|
||||
assert_eq!(validate_free_device(0x1000, 8, 1), Ok(()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_next_event() {
|
||||
assert_eq!(validate_get_next_event(0, 48), Err(EINVAL));
|
||||
assert_eq!(validate_get_next_event(0x1000, 40), Err(EINVAL));
|
||||
assert_eq!(validate_get_next_event(0x1000, 48), Ok(()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_free_swap_group() {
|
||||
assert_eq!(validate_free_swap_group(0, 12, 1, 1), Err(EINVAL));
|
||||
assert_eq!(validate_free_swap_group(0x1000, 40, 1, 1), Err(EINVAL));
|
||||
assert_eq!(validate_free_swap_group(0x1000, 12, 0, 1), Err(EINVAL));
|
||||
assert_eq!(validate_free_swap_group(0x1000, 12, 1, 0), Err(EINVAL));
|
||||
assert_eq!(validate_free_swap_group(0x1000, 12, 1, 2), Ok(()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_grab_ownership() {
|
||||
let mut tbl = OwnershipTable::new();
|
||||
// File 1 grabs device 0 (handle 1)
|
||||
assert_eq!(tbl.grab(32, 1, 1), Err(EINVAL));
|
||||
assert_eq!(tbl.grab(0, 1, 0), Err(EINVAL));
|
||||
assert_eq!(tbl.grab(0, 1, 1), Ok(()));
|
||||
assert!(tbl.is_owner(0, 1, 1));
|
||||
// Repeated grab by current owner succeeds idempotently
|
||||
assert_eq!(tbl.grab(0, 1, 1), Ok(()));
|
||||
// Competing file 2 is rejected with -EBUSY
|
||||
assert_eq!(tbl.grab(0, 2, 1), Err(EBUSY));
|
||||
// Non-owner release is rejected with -EPERM and preserves file 1 ownership
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_release_ownership() {
|
||||
let mut tbl = OwnershipTable::new();
|
||||
assert_eq!(tbl.grab(0, 1, 1), Ok(()));
|
||||
assert_eq!(tbl.release(0, 2, 1), Err(EPERM));
|
||||
assert!(tbl.is_owner(0, 1, 1));
|
||||
// Owner release succeeds and allows file 2 to grab
|
||||
assert_eq!(tbl.release(0, 1, 1), Ok(()));
|
||||
assert!(!tbl.is_owner(0, 1, 1));
|
||||
assert_eq!(tbl.grab(0, 2, 7), Ok(()));
|
||||
assert!(tbl.is_owner(0, 2, 7));
|
||||
// File close releases all devices owned by file 2
|
||||
tbl.release_all_for_file(2);
|
||||
tbl.release_device(2, 7);
|
||||
assert!(!tbl.is_owner(0, 2, 7));
|
||||
assert_eq!(tbl.grab(0, 2, 9), Ok(()));
|
||||
tbl.release_all_for_file(2);
|
||||
assert!(!tbl.is_owner(0, 2, 9));
|
||||
}
|
||||
}
|
||||
|
||||
@ -577,8 +577,13 @@ def audit_nvkms(stock_dir: pathlib.Path, open_gpu_root: pathlib.Path) -> list[di
|
||||
# Rust FFI symbol AND the corresponding dedicated unit test exists in nvkms/mod.rs.
|
||||
verified_ffi_tests: dict[int, tuple[str, str]] = {
|
||||
0: ("nv_rs_nvkms_validate_alloc_device(", "test_alloc_device"),
|
||||
1: ("nv_rs_nvkms_validate_free_device(", "test_free_device"),
|
||||
32: ("nv_rs_nvkms_validate_get_next_event(", "test_get_next_event"),
|
||||
33: ("nv_rs_nvkms_validate_event_interest(", "test_declare_event_interest"),
|
||||
38: ("nv_rs_nvkms_ownership_grab(", "test_grab_ownership"),
|
||||
39: ("nv_rs_nvkms_ownership_release(", "test_release_ownership"),
|
||||
46: ("nv_rs_nvkms_validate_alloc_swap_group(", "test_alloc_swap_group"),
|
||||
47: ("nv_rs_nvkms_validate_free_swap_group(", "test_free_swap_group"),
|
||||
65: ("nv_rs_nvkms_validate_fbcon_disabled_userspace(", "test_fbcon_disabled_userspace"),
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user