source: mainline/uspace/drv/bus/usb/xhci/hw_struct/common.h@ ca22536

Last change on this file since ca22536 was 590cb6d2, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 22 months ago

Remove some inappropriate uses of attribute((packed))

attribute((packed)) means "ignore all alignment requirements
on members of this structure". This is useful if one needs to
map onto a structure in memory that has misaligned fields on purpose,
but those cases are extremely rare.

The side effect of the attribute is that taking a pointer to any
field longer than a single byte, and dereferencing that pointer,
is unsound and may crash the program on architectures that care
about memory alignment when reading/writing multibyte values.

Newer GCC versions can detect some of those unsafe cases and
produce a warning for it. This commit only removes those cases.

However, most, if not all, uses of ((packed)) in HelenOS
are unnecessary and a product of misunderstanding what the
attribute actually does. A common misconception is that it is
needed to avoid compiler adding arbitrary padding into the
structure, but that is simply not true. There is exactly one
correct memory layout for any C structure, because there must
be one layout for binary interoperability to exist and the one
everyone uses (except perhaps some goblin who just wants to break
things for fun) is the trivial best layout possible with given
constraints.

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2 * Copyright (c) 2018 Ondrej Hlavaty
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup drvusbxhci
30 * @{
31 */
32/** @file
33 * @brief Common macros for HW structures.
34 *
35 * There are a lot of data structures that are defined on bit-basis.
36 * Therefore, we provide macros to define getters and setters.
37 */
38
39#ifndef XHCI_COMMON_H
40#define XHCI_COMMON_H
41
42#include <assert.h>
43#include <bitops.h>
44#include <byteorder.h>
45#include <ddi.h>
46#include <errno.h>
47#include <fibril.h>
48
49#define host2xhci(size, val) host2uint##size##_t_le((val))
50#define xhci2host(size, val) uint##size##_t_le2host((val))
51
52/**
53 * 4 bytes, little-endian.
54 */
55typedef ioport32_t xhci_dword_t;
56
57/**
58 * 8 bytes, little-endian.
59 */
60typedef volatile uint64_t xhci_qword_t;
61
62#define XHCI_DWORD_EXTRACT(field, hi, lo) \
63 (BIT_RANGE_EXTRACT(uint32_t, hi, lo, xhci2host(32, field)))
64#define XHCI_QWORD_EXTRACT(field, hi, lo) \
65 (BIT_RANGE_EXTRACT(uint64_t, hi, lo, xhci2host(64, field)))
66
67/**
68 * Common base for setters on xhci_dword_t storage.
69 *
70 * Not thread-safe, proper synchronization over this dword must be assured.
71 */
72static inline void xhci_dword_set_bits(xhci_dword_t *storage, uint32_t value,
73 unsigned hi, unsigned lo)
74{
75 const uint32_t mask = host2xhci(32, BIT_RANGE(uint32_t, hi, lo));
76 const uint32_t set = host2xhci(32, value << lo);
77 *storage = (*storage & ~mask) | set;
78}
79
80/**
81 * Setter for whole qword.
82 */
83static inline void xhci_qword_set(xhci_qword_t *storage, uint64_t value)
84{
85 *storage = host2xhci(64, value);
86}
87
88static inline void xhci_qword_set_bits(xhci_qword_t *storage, uint64_t value,
89 unsigned hi, unsigned lo)
90{
91 const uint64_t mask = host2xhci(64, BIT_RANGE(uint64_t, hi, lo));
92 const uint64_t set = host2xhci(64, value << lo);
93 *storage = (*storage & ~mask) | set;
94}
95
96static inline int xhci_reg_wait(xhci_dword_t *reg, uint32_t mask,
97 uint32_t expected)
98{
99 mask = host2xhci(32, mask);
100 expected = host2xhci(32, expected);
101
102 unsigned retries = 100;
103 uint32_t value = *reg & mask;
104
105 for (; retries > 0 && value != expected; --retries) {
106 fibril_usleep(10000);
107 value = *reg & mask;
108 }
109
110 return value == expected ? EOK : ETIMEOUT;
111}
112
113#endif
114
115/**
116 * @}
117 */
Note: See TracBrowser for help on using the repository browser.