1 // Copyright 2017 The Fuchsia Authors
2 //
3 // Use of this source code is governed by a MIT-style
4 // license that can be found in the LICENSE file or at
5 // https://opensource.org/licenses/MIT
6 
7 #pragma once
8 
9 #include <zircon/compiler.h>
10 #include <zircon/syscalls/resource.h>
11 #include <zircon/types.h>
12 
13 // Resource constants (ZX_RSRC_KIND_..., etc) are located
14 // in system/public/zircon/syscalls/resource.h
15 
16 // Determines if this handle is to a resource of the specified
17 // kind *or* to the root resource, which can stand in for any kind.
18 // Used to provide access to privileged syscalls.
19 zx_status_t validate_resource(zx_handle_t handle, uint32_t kind);
20 
21 // Validates a resource based on type and low/high range;
22 zx_status_t validate_ranged_resource(zx_handle_t handle, uint32_t kind, uint64_t base, size_t len);
23 
24 #if ARCH_X86
25 // Validates enabling ioport access bits for a given process based on a resource handle
validate_resource_ioport(zx_handle_t handle,uint64_t base,size_t len)26 static inline zx_status_t validate_resource_ioport(zx_handle_t handle, uint64_t base, size_t len) {
27     return validate_ranged_resource(handle, ZX_RSRC_KIND_IOPORT, base, len);
28 }
29 #endif
30 
31 // Validates mapping an MMIO range based on a resource handle
validate_resource_mmio(zx_handle_t handle,uint64_t base,size_t len)32 static inline zx_status_t validate_resource_mmio(zx_handle_t handle, uint64_t base, size_t len) {
33     return validate_ranged_resource(handle, ZX_RSRC_KIND_MMIO, base, len);
34 }
35 
36 // Validates creation of an interrupt object based on a resource handle
validate_resource_irq(zx_handle_t handle,uint32_t irq)37 static inline zx_status_t validate_resource_irq(zx_handle_t handle, uint32_t irq) {
38     return validate_ranged_resource(handle, ZX_RSRC_KIND_IRQ, irq, 1);
39 }
40 
41 // Validates access to a SMC service call number based on a resource handle
validate_resource_smc(zx_handle_t handle,uint64_t service_call_num)42 static inline zx_status_t validate_resource_smc(zx_handle_t handle, uint64_t service_call_num) {
43     return validate_ranged_resource(handle, ZX_RSRC_KIND_SMC, service_call_num, 1);
44 }
45