1/*
2 * Copyright (c) 2013, ETH Zurich.
3 * All rights reserved.
4 *
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
8 */
9
10#ifndef BULK_TRANSFER_BUFFER_H
11#define BULK_TRANSFER_BUFFER_H
12
13#include <bulk_transfer/bulk_transfer.h>
14
15/**
16 * checks if a given buffer is read only
17 *
18 * @param buffer    the buffer to check the read only access
19 *
20 * @return  true    if the buffer is read only
21 *          false   otherwise (rw or invalid)
22 */
23static inline uint8_t bulk_buffer_is_read_only(struct bulk_buffer *buffer) {
24    return ((buffer->state == BULK_BUFFER_READ_ONLY)
25                    || (buffer->state == BULK_BUFFER_RO_OWNED));
26}
27
28/**
29 * checks if the given buffer copy can be released
30 *
31 * @param buffer    the buffer to check the read only access
32 *
33 * @return  true    if the buffer copy can be released
34 *          false   otherwise (there are still references out there)
35 */
36static inline uint8_t bulk_buffer_can_release(struct bulk_buffer *buffer) {
37    return (buffer->local_ref_count == 0);
38}
39
40/**
41 * checks if the supplied size is valid for a buffer that is:
42 * - at least of size BASE_PAGE_SIZE
43 * - a power of two
44 */
45static inline uint8_t bulk_buffer_check_size (size_t x)
46{
47  return ((x != 0) && !(x & (x - 1)) && (x >= BASE_PAGE_SIZE));
48}
49
50/**
51 * does the mapping of the buffer according to the base address, capability
52 * and offset specified in the buffer struct.
53 *
54 * @param buf   the buffer to map
55 */
56errval_t bulk_buffer_map(struct bulk_buffer *buf);
57
58
59/**
60 * does the unmapping of a single buffer according to the trust level,
61 * - if the channel is fully trusted, this results in a no-op.
62 * - otherwise, the mapping is removed
63 *
64 * This function does not revoke or delete any capabilities
65 *
66 * @param buf   the buffer to unmap
67 */
68errval_t bulk_buffer_unmap(struct bulk_buffer *buf);
69
70
71/**
72 * changes the state of the buffer
73 *
74 * @param buffer    the buffer to change the state
75 * @param state     new state to transition the buffer to
76 */
77errval_t bulk_buffer_change_state(struct bulk_buffer       *buffer,
78                                  enum bulk_buffer_state    state);
79
80/**
81 * checks if the buffer is owned by the calling domain
82 *
83 * @param buffer   buffer to check for ownership
84 */
85uint8_t bulk_buffer_is_owner(struct bulk_buffer *buf);
86
87
88/**
89 * checks if the buffer is a read only copy
90 *
91 * @param buffer    the buffer to check
92 *
93 * @return true     if the buffer is a read only copy
94 *         false    if the buffer is not a copy
95 */
96uint8_t bulk_buffer_is_copy(struct bulk_buffer *buffer);
97
98
99/**
100 * checks if the buffer is valid
101 *
102 * @param buffer    the buffer to check
103 *
104 * @return true     if the buffer is valid
105 *         false    if the buffer is not valid
106 */
107uint8_t bulk_buffer_is_valid(struct bulk_buffer *buffer);
108
109/**
110 * Sets a cap + offset pair for a buffer.
111 *
112 * @param buffer     the buffer
113 * @param cap        cap to assign
114 * @param cap_offset offset in the cap
115 */
116errval_t bulk_buffer_assign_cap(struct bulk_buffer *buffer,
117                                struct capref       cap,
118                                size_t              cap_offset);
119
120
121
122#endif // ndef BULK_TRANSFER_BUFFER_H
123
124