1/*
2 * Copyright (c) 2015-2016, Intel Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** \file
30 * \brief Castle: multi-tenant repeat engine, data structures.
31 */
32
33#ifndef NFA_CASTLE_INTERNAL_H
34#define NFA_CASTLE_INTERNAL_H
35
36#include "ue2common.h"
37#include "repeat_internal.h"
38
39struct SubCastle {
40 ReportID report; //!< report to raise on match
41 u32 fullStateOffset; //!< offset within full state (scratch)
42 u32 streamStateOffset; //!< offset within stream state
43 u32 repeatInfoOffset; //!< offset of RepeatInfo structure
44 // relative to the start of SubCastle
45 u32 exclusiveId; //!< exclusive group id of this SubCastle,
46 // set to the number of SubCastles in Castle
47 // if it is not exclusive
48};
49
50#define CASTLE_DOT 0
51#define CASTLE_VERM 1
52#define CASTLE_NVERM 2
53#define CASTLE_SHUFTI 3
54#define CASTLE_TRUFFLE 4
55
56enum ExclusiveType {
57 NOT_EXCLUSIVE, //!< no subcastles are exclusive
58 EXCLUSIVE, //!< a subset of subcastles are exclusive
59 PURE_EXCLUSIVE //!< all subcastles are exclusive
60};
61
62/**
63 * \brief Castle engine structure.
64 *
65 * A Castle is a collection of repeats that all share the same character
66 * reachability.
67 *
68 * The whole engine is laid out in memory as:
69 *
70 * - struct NFA
71 * - struct Castle
72 * - struct SubCastle[numRepeats]
73 * - tables for sparse model repeats
74 * - sparse iterator for subcastles that may be stale
75 *
76 * Castle stores an "active repeats" multibit in stream state, followed by the
77 * packed repeat state for each SubCastle. If there are both exclusive and
78 * non-exclusive SubCastle groups, we use an active id for each exclusive group
79 * and a multibit for the non-exclusive group. We also store an "active
80 * exclusive groups" multibit for exclusive groups. If all SubCastles are mutual
81 * exclusive, we remove "active repeats" multibit from stream state.
82 * * Castle stream state:
83 * *
84 * * |---|
85 * * | | active subengine id for exclusive group 1
86 * * |---|
87 * * | | active subengine id for exclusive group 2(if necessary)
88 * * |---|
89 * * ...
90 * * |---|
91 * * | | "active repeats" multibit for non-exclusive subcastles
92 * * | | (if not all subcastles are exclusive)
93 * * |---|
94 * * | | active multibit for exclusive groups
95 * * | |
96 * * |---|
97 * * ||-|| common pool of stream state for exclusive group 1
98 * * ||-||
99 * * |---|
100 * * ||-|| common pool of stream state for exclusive group 2(if necessary)
101 * * ||-||
102 * * |---|
103 * * ...
104 * * |---|
105 * * | | stream state for each non-exclusive subcastles
106 * * ...
107 * * | |
108 * * |---|
109 *
110 * In full state (stored in scratch space) it stores a temporary multibit over
111 * the repeats (used by \ref castleMatchLoop), followed by the repeat control
112 * blocks for each SubCastle.
113 */
114struct ALIGN_AVX_DIRECTIVE Castle {
115 u32 numRepeats; //!< number of repeats in Castle
116 u32 numGroups; //!< number of exclusive groups
117 u8 type; //!< tells us which scanning mechanism (below) to use
118 u8 exclusive; //!< tells us if there are mutual exclusive SubCastles
119 u8 activeIdxSize; //!< number of bytes in stream state to store
120 // active SubCastle id for exclusive mode
121 u32 activeOffset; //!< offset to active multibit for non-exclusive
122 // SubCastles
123 u32 staleIterOffset; //!< offset to a sparse iterator to check for stale
124 // sub castles
125 u32 groupIterOffset; //!< offset to a iterator to check the aliveness of
126 // exclusive groups
127
128 union {
129 struct {
130 char c;
131 } verm;
132 struct {
133 m128 mask_lo;
134 m128 mask_hi;
135 } shuf;
136 struct {
137 m128 mask1;
138 m128 mask2;
139 } truffle;
140 } u;
141};
142
143#endif // NFA_CASTLE_INTERNAL_H
144