aboutsummaryrefslogtreecommitdiffstats
path: root/frontends/ast/ast.h
blob: 46d482f1a9b04fe30b8763af0efb9727ef4ec3d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* -*- c++ -*-
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2012  Clifford Wolf <clifford@clifford.at>
 *
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 *  ---
 *
 *  This is the AST frontend library.
 *
 *  The AST frontend library is not a frontend on it's own but provides a
 *  generic abstract syntax tree (AST) abstraction for HDL code and can be
 *  used by HDL frontends. See "ast.h" for an overview of the API and the
 *  Verilog frontend for an usage example.
 *
 */

#ifndef AST_H
#define AST_H

#include "kernel/rtlil.h"
#include <stdint.h>
#include <set>

YOSYS_NAMESPACE_BEGIN

namespace AST
{
	// all node types, type2str() must be extended
	// whenever a new node type is added here
	enum AstNodeType
	{
		AST_NONE,
		AST_DESIGN,
		AST_MODULE,
		AST_TASK,
		AST_FUNCTION,
		AST_DPI_FUNCTION,

		AST_WIRE,
		AST_MEMORY,
		AST_AUTOWIRE,
		AST_PARAMETER,
		AST_LOCALPARAM,
		AST_DEFPARAM,
		AST_PARASET,
		AST_ARGUMENT,
		AST_RANGE,
		AST_MULTIRANGE,
		AST_CONSTANT,
		AST_REALVALUE,
		AST_CELLTYPE,
		AST_IDENTIFIER,
		AST_PREFIX,
		AST_ASSERT,
		AST_ASSUME,
		AST_LIVE,
		AST_FAIR,
		AST_COVER,

		AST_FCALL,
		AST_TO_BITS,
		AST_TO_SIGNED,
		AST_TO_UNSIGNED,
		AST_CONCAT,
		AST_REPLICATE,
		AST_BIT_NOT,
		AST_BIT_AND,
		AST_BIT_OR,
		AST_BIT_XOR,
		AST_BIT_XNOR,
		AST_REDUCE_AND,
		AST_REDUCE_OR,
		AST_REDUCE_XOR,
		AST_REDUCE_XNOR,
		AST_REDUCE_BOOL,
		AST_SHIFT_LEFT,
		AST_SHIFT_RIGHT,
		AST_SHIFT_SLEFT,
		AST_SHIFT_SRIGHT,
		AST_LT,
		AST_LE,
		AST_EQ,
		AST_NE,
		AST_EQX,
		AST_NEX,
		AST_GE,
		AST_GT,
		AST_ADD,
		AST_SUB,
		AST_MUL,
		AST_DIV,
		AST_MOD,
		AST_POW,
		AST_POS,
		AST_NEG,
		AST_LOGIC_AND,
		AST_LOGIC_OR,
		AST_LOGIC_NOT,
		AST_TERNARY,
		AST_MEMRD,
		AST_MEMWR,
		AST_MEMINIT,

		AST_TCALL,
		AST_ASSIGN,
		AST_CELL,
		AST_PRIMITIVE,
		AST_CELLARRAY,
		AST_ALWAYS,
		AST_INITIAL,
		AST_BLOCK,
		AST_ASSIGN_EQ,
		AST_ASSIGN_LE,
		AST_CASE,
		AST_COND,
		AST_CONDX,
		AST_CONDZ,
		AST_DEFAULT,
		AST_FOR,
		AST_WHILE,
		AST_REPEAT,

		AST_GENVAR,
		AST_GENFOR,
		AST_GENIF,
		AST_GENCASE,
		AST_GENBLOCK,

		AST_POSEDGE,
		AST_NEGEDGE,
		AST_EDGE,

		AST_INTERFACE,
		AST_INTERFACEPORT,
		AST_INTERFACEPORTTYPE,
		AST_MODPORT,
		AST_MODPORTMEMBER,
		AST_PACKAGE
	};

	// convert an node type to a string (e.g. for debug output)
	std::string type2str(AstNodeType type);

	// The AST is built using instances of this struct
	struct AstNode
	{
		// for dict<> and pool<>
		unsigned int hashidx_;
		unsigned int hash() const { return hashidx_; }

		// this nodes type
		AstNodeType type;

		// the list of child nodes for this node
		std::vector<AstNode*> children;

		// the list of attributes assigned to this node
		std::map<RTLIL::IdString, AstNode*> attributes;
		bool get_bool_attribute(RTLIL::IdString id);

		// node content - most of it is unused in most node types
		std::string str;
		std::vector<RTLIL::State> bits;
		bool is_input, is_output, is_reg, is_logic, is_signed, is_string, is_wand, is_wor, range_valid, range_swapped, was_checked, is_unsized;
		int port_id, range_left, range_right;
		uint32_t integer;
		double realvalue;

		// if this is a multirange memory then this vector contains offset and length of each dimension
		std::vector<int> multirange_dimensions;

		// this is set by simplify and used during RTLIL generation
		AstNode *id2ast;

		// this is used by simplify to detect if basic analysis has been performed already on the node
		bool basic_prep;

		// this is the original sourcecode location that resulted in this AST node
		// it is automatically set by the constructor using AST::current_filename and
		// the AST::get_line_num() callback function.
		std::string filename;
		int linenum;

		// creating and deleting nodes
		AstNode(AstNodeType type = AST_NONE, AstNode *child1 = NULL, AstNode *child2 = NULL, AstNode *child3 = NULL);
		AstNode *clone() const;
		void cloneInto(AstNode *other) const;
		void delete_children();
		~AstNode();

		enum mem2reg_flags
		{
			/* status flags */
			MEM2REG_FL_ALL       = 0x00000001,
			MEM2REG_FL_ASYNC     = 0x00000002,
			MEM2REG_FL_INIT      = 0x00000004,

			/* candidate flags */
			MEM2REG_FL_FORCED    = 0x00000100,
			MEM2REG_FL_SET_INIT  = 0x00000200,
			MEM2REG_FL_SET_ELSE  = 0x00000400,
			MEM2REG_FL_SET_ASYNC = 0x00000800,
			MEM2REG_FL_EQ2       = 0x00001000,
			MEM2REG_FL_CMPLX_LHS = 0x00002000,
			MEM2REG_FL_CONST_LHS = 0x00004000,
			MEM2REG_FL_VAR_LHS   = 0x00008000,

			/* proc flags */
			MEM2REG_FL_EQ1       = 0x01000000,
		};

		// simplify() creates a simpler AST by unrolling for-loops, expanding generate blocks, etc.
		// it also sets the id2ast pointers so that identifier lookups are fast in genRTLIL()
		bool simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage, int width_hint, bool sign_hint, bool in_param);
		AstNode *readmem(bool is_readmemh, std::string mem_filename, AstNode *memory, int start_addr, int finish_addr, bool unconditional_init);
		void expand_genblock(std::string index_var, std::string prefix, std::map<std::string, std::string> &name_map);
		void replace_ids(const std::string &prefix, const std::map<std::string, std::string> &rules);
		void mem2reg_as_needed_pass1(dict<AstNode*, pool<std::string>> &mem2reg_places,
				dict<AstNode*, uint32_t> &mem2reg_flags, dict<AstNode*, uint32_t> &proc_flags, uint32_t &status_flags);
		bool mem2reg_as_needed_pass2(pool<AstNode*> &mem2reg_set, AstNode *mod, AstNode *block, AstNode *&async_block);
		bool mem2reg_check(pool<AstNode*> &mem2reg_set);
		void mem2reg_remove(pool<AstNode*> &mem2reg_set, vector<AstNode*> &delnodes);
		void meminfo(int &mem_width, int &mem_size, int &addr_bits);

		// additional functionality for evaluating constant functions
		struct varinfo_t { RTLIL::Const val; int offset; bool is_signed; };
		bool has_const_only_constructs(bool &recommend_const_eval);
		void replace_variables(std::map<std::string, varinfo_t> &variables, AstNode *fcall);
		AstNode *eval_const_function(AstNode *fcall);
		bool is_simple_const_expr();

		// create a human-readable text representation of the AST (for debugging)
		void dumpAst(FILE *f, std::string indent) const;
		void dumpVlog(FILE *f, std::string indent) const;

		// used by genRTLIL() for detecting expression width and sign
		void detectSignWidthWorker(int &width_hint, bool &sign_hint, bool *found_real = NULL);
		void detectSignWidth(int &width_hint, bool &sign_hint, bool *found_real = NULL);

		// create RTLIL code for this AST node
		// for expressions the resulting signal vector is returned
		// all generated cell instances, etc. are written to the RTLIL::Module pointed to by AST_INTERNAL::current_module
		RTLIL::SigSpec genRTLIL(int width_hint = -1, bool sign_hint = false);
		RTLIL::SigSpec genWidthRTLIL(int width, const dict<RTLIL::SigBit, RTLIL::SigBit> *new_subst_ptr = NULL);

		// compare AST nodes
		bool operator==(const AstNode &other) const;
		bool operator!=(const AstNode &other) const;
		bool contains(const AstNode *other) const;

		// helper functions for creating AST nodes for constants
		static AstNode *mkconst_int(uint32_t v, bool is_signed, int width = 32);
		static AstNode *mkconst_bits(const std::vector<RTLIL::State> &v, bool is_signed, bool is_unsized);
		static AstNode *mkconst_bits(const std::vector<RTLIL::State> &v, bool is_signed);
		static AstNode *mkconst_str(const std::vector<RTLIL::State> &v);
		static AstNode *mkconst_str(const std::string &str);

		// helper function for creating sign-extended const objects
		RTLIL::Const bitsAsConst(int width, bool is_signed);
		RTLIL::Const bitsAsConst(int width = -1);
		RTLIL::Const bitsAsUnsizedConst(int width);
		RTLIL::Const asAttrConst();
		RTLIL::Const asParaConst();
		uint64_t asInt(bool is_signed);
		bool bits_only_01() const;
		bool asBool() const;

		// helper functions for real valued const eval
		int isConst() const; // return '1' for AST_CONSTANT and '2' for AST_REALVALUE
		double asReal(bool is_signed);
		RTLIL::Const realAsConst(int width);
	};

	// process an AST tree (ast must point to an AST_DESIGN node) and generate RTLIL code
	void process(RTLIL::Design *design, AstNode *ast, bool dump_ast1, bool dump_ast2, bool no_dump_ptr, bool dump_vlog1, bool dump_vlog2, bool dump_rtlil, bool nolatches, bool nomeminit,
			bool nomem2reg, bool mem2reg, bool noblackbox, bool lib, bool nowb, bool noopt, bool icells, bool nooverwrite, bool overwrite, bool defer, bool autowire);

	// parametric modules are supported directly by the AST library
	// therefore we need our own derivate of RTLIL::Module with overloaded virtual functions
	struct AstModule : RTLIL::Module {
		AstNode *ast;
		bool nolatches, nomeminit, nomem2reg, mem2reg, noblackbox, lib, nowb, noopt, icells, autowire;
		~AstModule() YS_OVERRIDE;
		RTLIL::IdString derive(RTLIL::Design *design, dict<RTLIL::IdString, RTLIL::Const> parameters, bool mayfail) YS_OVERRIDE;
		RTLIL::IdString derive(RTLIL::Design *design, dict<RTLIL::IdString, RTLIL::Const> parameters, dict<RTLIL::IdString, RTLIL::Module*> interfaces, dict<RTLIL::IdString, RTLIL::IdString> modports, bool mayfail) YS_OVERRIDE;
		std::string derive_common(RTLIL::Design *design, dict<RTLIL::IdString, RTLIL::Const> parameters, AstNode **new_ast_out, bool mayfail);
		void reprocess_module(RTLIL::Design *design, dict<RTLIL::IdString, RTLIL::Module *> local_interfaces) YS_OVERRIDE;
		RTLIL::Module *clone() const YS_OVERRIDE;
	};

	// this must be set by the language frontend before parsing the sources
	// the AstNode constructor then uses current_filename and get_line_num()
	// to initialize the filename and linenum properties of new nodes
	extern std::string current_filename;
	extern void (*set_line_num)(int);
	extern int (*get_line_num)();

	// set set_line_num and get_line_num to internal dummy functions (done by simplify() and AstModule::derive
	// to control the filename and linenum properties of new nodes not generated by a frontend parser)
	void use_internal_line_num();

	// call a DPI function
	AstNode *dpi_call(const std::string &rtype, const std::string &fname, const std::vector<std::string> &argtypes, const std::vector<AstNode*> &args);

	// Helper functions related to handling SystemVerilog interfaces
	std::pair<std::string,std::string> split_modport_from_type(std::string name_type);
	AstNode * find_modport(AstNode *intf, std::string name);
	void explode_interface_port(AstNode *module_ast, RTLIL::Module * intfmodule, std::string intfname, AstNode *modport);
}

namespace AST_INTERNAL
{
	// internal state variables
	extern bool flag_dump_ast1, flag_dump_ast2, flag_no_dump_ptr, flag_dump_rtlil, flag_nolatches, flag_nomeminit;
	extern bool flag_nomem2reg, flag_mem2reg, flag_lib, flag_noopt, flag_icells, flag_autowire;
	extern AST::AstNode *current_ast, *current_ast_mod;
	extern std::map<std::string, AST::AstNode*> current_scope;
	extern const dict<RTLIL::SigBit, RTLIL::SigBit> *genRTLIL_subst_ptr;
	extern RTLIL::SigSpec ignoreThisSignalsInInitial;
	extern AST::AstNode *current_always, *current_top_block, *current_block, *current_block_child;
	extern AST::AstModule *current_module;
	extern bool current_always_clocked;
	struct ProcessGenerator;
}

YOSYS_NAMESPACE_END

#endif
pan>\n", pfn); goto out; } page = frame_table + pfn; /* Each frame must belong to the requesting domain. */ if ( (page->flags & PG_domain_mask) != p->domain ) { DPRINTK("bad domain: expected %d, got %ld\n", p->domain, page->flags & PG_domain_mask); goto out; } /* If reading into the frame, the frame must be writeable. */ if ( writeable_buffer && ((page->flags & PG_type_mask) != PGT_writeable_page) ) { DPRINTK("non-writeable page passed for block read\n"); goto out; } } rc = 1; out: return rc; } static void __lock_buffer(unsigned long buffer, unsigned short size, int writeable_buffer) { unsigned long pfn; struct pfn_info *page; for ( pfn = buffer >> PAGE_SHIFT; pfn < ((buffer + size + PAGE_SIZE - 1) >> PAGE_SHIFT); pfn++ ) { page = frame_table + pfn; if ( writeable_buffer ) get_page_type(page); get_page_tot(page); } } static void unlock_buffer(struct task_struct *p, unsigned long buffer, unsigned short size, int writeable_buffer) { unsigned long pfn, flags; struct pfn_info *page; spin_lock_irqsave(&p->page_lock, flags); for ( pfn = buffer >> PAGE_SHIFT; pfn < ((buffer + size + PAGE_SIZE - 1) >> PAGE_SHIFT); pfn++ ) { page = frame_table + pfn; if ( writeable_buffer && (put_page_type(page) == 0) ) page->flags &= ~PG_type_mask; put_page_tot(page); } spin_unlock_irqrestore(&p->page_lock, flags); } static int do_block_io_op_domain(struct task_struct *p, int max_to_do) { blk_ring_t *blk_ring = p->blk_ring_base; int i, more_to_do = 0; /* * Take items off the comms ring, taking care not to catch up * with the response-producer index. */ for ( i = p->blk_req_cons; (i != blk_ring->req_prod) && (((p->blk_resp_prod-i) & (BLK_RING_SIZE-1)) != 1); i = BLK_RING_INC(i) ) { if ( (max_to_do-- == 0) || (atomic_read(&nr_pending) == MAX_PENDING_REQS) ) { more_to_do = 1; break; } switch ( blk_ring->ring[i].req.operation ) { case XEN_BLOCK_READ: case XEN_BLOCK_WRITE: dispatch_rw_block_io(p, i); break; case XEN_BLOCK_PROBE_BLK: dispatch_probe_blk(p, i); break; case XEN_BLOCK_PROBE_SEG: dispatch_probe_seg(p, i); break; case XEN_BLOCK_DEBUG: dispatch_debug_block_io(p, i); break; case XEN_BLOCK_SEG_CREATE: dispatch_create_segment(p, i); break; case XEN_BLOCK_SEG_DELETE: dispatch_delete_segment(p, i); break; default: DPRINTK("error: unknown block io operation [%d]\n", blk_ring->ring[i].req.operation); make_response(p, blk_ring->ring[i].req.id, blk_ring->ring[i].req.operation, 1); break; } } p->blk_req_cons = i; return more_to_do; } static void dispatch_debug_block_io(struct task_struct *p, int index) { DPRINTK("dispatch_debug_block_io: unimplemented\n"); } static void dispatch_create_segment(struct task_struct *p, int index) { blk_ring_t *blk_ring = p->blk_ring_base; unsigned long flags, buffer; xv_disk_t *xvd; int result; if ( p->domain != 0 ) { DPRINTK("dispatch_create_segment called by dom%d\n", p->domain); result = 1; goto out; } buffer = blk_ring->ring[index].req.buffer_and_sects[0] & ~0x1FF; spin_lock_irqsave(&p->page_lock, flags); if ( !__buffer_is_valid(p, buffer, sizeof(xv_disk_t), 1) ) { DPRINTK("Bad buffer in dispatch_create_segment\n"); spin_unlock_irqrestore(&p->page_lock, flags); result = 1; goto out; } __lock_buffer(buffer, sizeof(xv_disk_t), 1); spin_unlock_irqrestore(&p->page_lock, flags); xvd = phys_to_virt(buffer); result = xen_segment_create(xvd); unlock_buffer(p, buffer, sizeof(xv_disk_t), 1); out: make_response(p, blk_ring->ring[index].req.id, XEN_BLOCK_SEG_CREATE, result); } static void dispatch_delete_segment(struct task_struct *p, int index) { DPRINTK("dispatch_delete_segment: unimplemented\n"); } static void dispatch_probe_blk(struct task_struct *p, int index) { extern void ide_probe_devices(xen_disk_info_t *xdi); extern void scsi_probe_devices(xen_disk_info_t *xdi); blk_ring_t *blk_ring = p->blk_ring_base; xen_disk_info_t *xdi; unsigned long flags, buffer; int rc = 0; buffer = blk_ring->ring[index].req.buffer_and_sects[0] & ~0x1FF; spin_lock_irqsave(&p->page_lock, flags); if ( !__buffer_is_valid(p, buffer, sizeof(xen_disk_info_t), 1) ) { DPRINTK("Bad buffer in dispatch_probe_blk\n"); spin_unlock_irqrestore(&p->page_lock, flags); rc = 1; goto out; } __lock_buffer(buffer, sizeof(xen_disk_info_t), 1); spin_unlock_irqrestore(&p->page_lock, flags); xdi = phys_to_virt(buffer); ide_probe_devices(xdi); scsi_probe_devices(xdi); unlock_buffer(p, buffer, sizeof(xen_disk_info_t), 1); out: make_response(p, blk_ring->ring[index].req.id, XEN_BLOCK_PROBE_BLK, rc); } static void dispatch_probe_seg(struct task_struct *p, int index) { extern void xen_segment_probe(struct task_struct *, xen_disk_info_t *); blk_ring_t *blk_ring = p->blk_ring_base; xen_disk_info_t *xdi; unsigned long flags, buffer; int rc = 0; buffer = blk_ring->ring[index].req.buffer_and_sects[0] & ~0x1FF; spin_lock_irqsave(&p->page_lock, flags); if ( !__buffer_is_valid(p, buffer, sizeof(xen_disk_info_t), 1) ) { DPRINTK("Bad buffer in dispatch_probe_seg\n"); spin_unlock_irqrestore(&p->page_lock, flags); rc = 1; goto out; } __lock_buffer(buffer, sizeof(xen_disk_info_t), 1); spin_unlock_irqrestore(&p->page_lock, flags); xdi = phys_to_virt(buffer); xen_segment_probe(p, xdi); unlock_buffer(p, buffer, sizeof(xen_disk_info_t), 1); out: make_response(p, blk_ring->ring[index].req.id, XEN_BLOCK_PROBE_SEG, rc); } static void dispatch_rw_block_io(struct task_struct *p, int index) { extern void ll_rw_block(int rw, int nr, struct buffer_head * bhs[]); blk_ring_t *blk_ring = p->blk_ring_base; blk_ring_req_entry_t *req = &blk_ring->ring[index].req; struct buffer_head *bh; int operation = (req->operation == XEN_BLOCK_WRITE) ? WRITE : READ; unsigned short nr_sects; unsigned long buffer, flags; int i, tot_sects; pending_req_t *pending_req; /* We map virtual scatter/gather segments to physical segments. */ int new_segs, nr_psegs = 0; phys_seg_t phys_seg[MAX_BLK_SEGS * 2]; spin_lock_irqsave(&p->page_lock, flags); /* Check that number of segments is sane. */ if ( (req->nr_segments == 0) || (req->nr_segments > MAX_BLK_SEGS) ) { DPRINTK("Bad number of segments in request (%d)\n", req->nr_segments); goto bad_descriptor; } /* * Check each address/size pair is sane, and convert into a * physical device and block offset. Note that if the offset and size * crosses a virtual extent boundary, we may end up with more * physical scatter/gather segments than virtual segments. */ for ( i = tot_sects = 0; i < req->nr_segments; i++, tot_sects += nr_sects ) { buffer = req->buffer_and_sects[i] & ~0x1FF; nr_sects = req->buffer_and_sects[i] & 0x1FF; if ( nr_sects == 0 ) { DPRINTK("zero-sized data request\n"); goto bad_descriptor; } if ( !__buffer_is_valid(p, buffer, nr_sects<<9, (operation==READ)) ) goto bad_descriptor; /* Get the physical device and block index. */ if ( (req->device & XENDEV_TYPE_MASK) == XENDEV_VIRTUAL ) { new_segs = xen_segment_map_request( &phys_seg[nr_psegs], p, operation, req->device, req->sector_number + tot_sects, buffer, nr_sects); if ( new_segs <= 0 ) goto bad_descriptor; } else { phys_seg[nr_psegs].dev = xendev_to_physdev(req->device); phys_seg[nr_psegs].sector_number = req->sector_number + tot_sects; phys_seg[nr_psegs].buffer = buffer; phys_seg[nr_psegs].nr_sects = nr_sects; if ( phys_seg[nr_psegs].dev == 0 ) goto bad_descriptor; new_segs = 1; } nr_psegs += new_segs; if ( nr_psegs >= (MAX_BLK_SEGS*2) ) BUG(); } /* Lock pages associated with each buffer head. */ for ( i = 0; i < nr_psegs; i++ ) __lock_buffer(phys_seg[i].buffer, phys_seg[i].nr_sects<<9, (operation==READ)); spin_unlock_irqrestore(&p->page_lock, flags); atomic_inc(&nr_pending); pending_req = pending_reqs + pending_ring[pending_cons]; PENDREQ_IDX_INC(pending_cons); pending_req->domain = p; pending_req->id = req->id; pending_req->operation = operation; pending_req->status = 0; atomic_set(&pending_req->pendcnt, nr_psegs); /* Now we pass each segment down to the real blkdev layer. */ for ( i = 0; i < nr_psegs; i++ ) { bh = kmem_cache_alloc(buffer_head_cachep, GFP_KERNEL); if ( bh == NULL ) panic("bh is null\n"); memset (bh, 0, sizeof (struct buffer_head)); bh->b_size = phys_seg[i].nr_sects << 9; bh->b_dev = phys_seg[i].dev; bh->b_rsector = phys_seg[i].sector_number; bh->b_data = phys_to_virt(phys_seg[i].buffer); bh->b_end_io = end_block_io_op; bh->pending_req = pending_req; if ( operation == WRITE ) { bh->b_state = (1 << BH_JBD) | (1 << BH_Mapped) | (1 << BH_Req) | (1 << BH_Dirty) | (1 << BH_Uptodate) | (1 << BH_Write); } else { bh->b_state = (1 << BH_Mapped) | (1 << BH_Read); } /* Dispatch a single request. We'll flush it to disc later. */ ll_rw_block(operation, 1, &bh); } return; bad_descriptor: spin_unlock_irqrestore(&p->page_lock, flags); make_response(p, req->id, req->operation, 1); } /****************************************************************** * MISCELLANEOUS SETUP / TEARDOWN / DEBUGGING */ kdev_t xendev_to_physdev(unsigned short xendev) { switch ( (xendev & XENDEV_TYPE_MASK) ) { case XENDEV_IDE: xendev &= XENDEV_IDX_MASK; if ( xendev >= NR_IDE_DEVS ) { DPRINTK("IDE device number out of range %d\n", xendev); goto fail; } return ide_devs[xendev]; case XENDEV_SCSI: xendev &= XENDEV_IDX_MASK; if ( xendev >= NR_SCSI_DEVS ) { DPRINTK("SCSI device number out of range %d\n", xendev); goto fail; } return scsi_devs[xendev]; default: DPRINTK("xendev_to_physdev: unknown device %d\n", xendev); } fail: return (kdev_t)0; } static void make_response(struct task_struct *p, unsigned long id, unsigned short op, unsigned long st) { unsigned long cpu_mask, flags; int position; blk_ring_t *blk_ring; /* Place on the response ring for the relevant domain. */ spin_lock_irqsave(&p->blk_ring_lock, flags); blk_ring = p->blk_ring_base; position = p->blk_resp_prod; blk_ring->ring[position].resp.id = id; blk_ring->ring[position].resp.operation = op; blk_ring->ring[position].resp.status = st; p->blk_resp_prod = blk_ring->resp_prod = BLK_RING_INC(position); spin_unlock_irqrestore(&p->blk_ring_lock, flags); /* Kick the relevant domain. */ cpu_mask = mark_guest_event(p, _EVENT_BLK_RESP); guest_event_notify(cpu_mask); } static void dump_blockq(u_char key, void *dev_id, struct pt_regs *regs) { struct task_struct *p; blk_ring_t *blk_ring ; printk("Dumping block queue stats: nr_pending = %d (prod=%d,cons=%d)\n", atomic_read(&nr_pending), pending_prod, pending_cons); p = current->next_task; do { if ( !is_idle_task(p) ) { printk("Domain: %d\n", p->domain); blk_ring = p->blk_ring_base; printk(" req_prod:%d, req_cons:%d resp_prod:%d/%d on_list=%d\n", blk_ring->req_prod, p->blk_req_cons, blk_ring->resp_prod, p->blk_resp_prod, __on_blkdev_list(p)); } p = p->next_task; } while (p != current); } /* Start-of-day initialisation for a new domain. */ void init_blkdev_info(struct task_struct *p) { if ( sizeof(*p->blk_ring_base) > PAGE_SIZE ) BUG(); p->blk_ring_base = (blk_ring_t *)get_free_page(GFP_KERNEL); clear_page(p->blk_ring_base); SHARE_PFN_WITH_DOMAIN(virt_to_page(p->blk_ring_base), p->domain); p->blkdev_list.next = NULL; memset(p->segment_list, 0, sizeof(p->segment_list)); p->segment_count = 0; /* Get any previously created segments. */ xen_refresh_segment_list(p); } /* End-of-day teardown for a domain. XXX Outstanding requests? */ void destroy_blkdev_info(struct task_struct *p) { remove_from_blkdev_list(p); UNSHARE_PFN(virt_to_page(p->blk_ring_base)); free_page((unsigned long)p->blk_ring_base); } void initialize_block_io () { int i; atomic_set(&nr_pending, 0); pending_prod = pending_cons = 0; memset(pending_reqs, 0, sizeof(pending_reqs)); for ( i = 0; i < MAX_PENDING_REQS; i++ ) pending_ring[i] = i; spin_lock_init(&io_schedule_list_lock); INIT_LIST_HEAD(&io_schedule_list); buffer_head_cachep = kmem_cache_create( "buffer_head_cache", sizeof(struct buffer_head), 0, SLAB_HWCACHE_ALIGN, NULL, NULL); xen_segment_initialize(); add_key_handler('b', dump_blockq, "dump xen ide blkdev stats"); }