aboutsummaryrefslogblamecommitdiffstats
path: root/arch/ia64/kvm/kvm_minstate.h
blob: b2bcaa2787aad228a9c24e99dca6bd35fda2422e (plain) (tree)

























                                                                               

                         








                                                                                   
                                                         















                                                                                   
                                                                         
                                                                         

                                                                         
                                                                         


                                                                         
                                                                         
                                                                         

                                                                         







































































































































































































                                                                                               
/*
 *  kvm_minstate.h: min save macros
 *  Copyright (c) 2007, Intel Corporation.
 *
 *  Xuefei Xu (Anthony Xu) (Anthony.xu@intel.com)
 *  Xiantao Zhang (xiantao.zhang@intel.com)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307 USA.
 *
 */


#include <asm/asmmacro.h>
#include <asm/types.h>
#include <asm/kregs.h>
#include <asm/kvm_host.h>

#include "asm-offsets.h"

#define KVM_MINSTATE_START_SAVE_MIN	     					\
	mov ar.rsc = 0;/* set enforced lazy mode, pl 0, little-endian, loadrs=0 */\
	;;									\
	mov.m r28 = ar.rnat;                                  			\
	addl r22 = VMM_RBS_OFFSET,r1;            /* compute base of RBS */	\
	;;									\
	lfetch.fault.excl.nt1 [r22];						\
	addl r1 = KVM_STK_OFFSET-VMM_PT_REGS_SIZE, r1;  \
	mov r23 = ar.bspstore;			/* save ar.bspstore */          \
	;;									\
	mov ar.bspstore = r22;				/* switch to kernel RBS */\
	;;									\
	mov r18 = ar.bsp;							\
	mov ar.rsc = 0x3;     /* set eager mode, pl 0, little-endian, loadrs=0 */



#define KVM_MINSTATE_END_SAVE_MIN						\
	bsw.1;          /* switch back to bank 1 (must be last in insn group) */\
	;;


#define PAL_VSA_SYNC_READ						\
	/* begin to call pal vps sync_read */				\
{.mii;									\
	add r25 = VMM_VPD_BASE_OFFSET, r21;				\
	nop 0x0;							\
	mov r24=ip;							\
	;;								\
}									\
{.mmb									\
	add r24=0x20, r24;						\
	ld8 r25 = [r25];      /* read vpd base */			\
	br.cond.sptk kvm_vps_sync_read;		/*call the service*/	\
	;;								\
};									\


#define KVM_MINSTATE_GET_CURRENT(reg)   mov reg=r21

/*
 * KVM_DO_SAVE_MIN switches to the kernel stacks (if necessary) and saves
 * the minimum state necessary that allows us to turn psr.ic back
 * on.
 *
 * Assumed state upon entry:
 *  psr.ic: off
 *  r31:	contains saved predicates (pr)
 *
 * Upon exit, the state is as follows:
 *  psr.ic: off
 *   r2 = points to &pt_regs.r16
 *   r8 = contents of ar.ccv
 *   r9 = contents of ar.csd
 *  r10 = contents of ar.ssd
 *  r11 = FPSR_DEFAULT
 *  r12 = kernel sp (kernel virtual address)
 *  r13 = points to current task_struct (kernel virtual address)
 *  p15 = TRUE if psr.i is set in cr.ipsr
 *  predicate registers (other than p2, p3, and p15), b6, r3, r14, r15:
 *	  preserved
 *
 * Note that psr.ic is NOT turned on by this macro.  This is so that
 * we can pass interruption state as arguments to a handler.
 */


#define PT(f) (VMM_PT_REGS_##f##_OFFSET)

#define KVM_DO_SAVE_MIN(COVER,SAVE_IFS,EXTRA)			\
	KVM_MINSTATE_GET_CURRENT(r16);  /* M (or M;;I) */	\
	mov r27 = ar.rsc;         /* M */			\
	mov r20 = r1;         /* A */				\
	mov r25 = ar.unat;        /* M */			\
	mov r29 = cr.ipsr;        /* M */			\
	mov r26 = ar.pfs;         /* I */			\
	mov r18 = cr.isr;         				\
	COVER;              /* B;; (or nothing) */		\
	;;							\
	tbit.z p0,p15 = r29,IA64_PSR_I_BIT;			\
	mov r1 = r16;						\
/*	mov r21=r16;	*/					\
	/* switch from user to kernel RBS: */			\
	;;							\
	invala;             /* M */				\
	SAVE_IFS;						\
	;;							\
	KVM_MINSTATE_START_SAVE_MIN				\
	adds r17 = 2*L1_CACHE_BYTES,r1;/* cache-line size */	\
	adds r16 = PT(CR_IPSR),r1;				\
	;;							\
	lfetch.fault.excl.nt1 [r17],L1_CACHE_BYTES;		\
	st8 [r16] = r29;      /* save cr.ipsr */		\
	;;							\
	lfetch.fault.excl.nt1 [r17];				\
	tbit.nz p15,p0 = r29,IA64_PSR_I_BIT;			\
	mov r29 = b0						\
	;;							\
	adds r16 = PT(R8),r1; /* initialize first base pointer */\
	adds r17 = PT(R9),r1; /* initialize second base pointer */\
	;;							\
.mem.offset 0,0; st8.spill [r16] = r8,16;			\
.mem.offset 8,0; st8.spill [r17] = r9,16;			\
	;;							\
.mem.offset 0,0; st8.spill [r16] = r10,24;			\
.mem.offset 8,0; st8.spill [r17] = r11,24;			\
	;;							\
	mov r9 = cr.iip;         /* M */			\
	mov r10 = ar.fpsr;        /* M */			\
	;;							\
	st8 [r16] = r9,16;    /* save cr.iip */			\
	st8 [r17] = r30,16;   /* save cr.ifs */			\
	sub r18 = r18,r22;    /* r18=RSE.ndirty*8 */		\
	;;							\
	st8 [r16] = r25,16;   /* save ar.unat */		\
	st8 [r17] = r26,16;    /* save ar.pfs */		\
	shl r18 = r18,16;     /* calu ar.rsc used for "loadrs" */\
	;;							\
	st8 [r16] = r27,16;   /* save ar.rsc */			\
	st8 [r17] = r28,16;   /* save ar.rnat */		\
	;;          /* avoid RAW on r16 & r17 */		\
	st8 [r16] = r23,16;   /* save ar.bspstore */		\
	st8 [r17] = r31,16;   /* save predicates */		\
	;;							\
	st8 [r16] = r29,16;   /* save b0 */			\
	st8 [r17] = r18,16;   /* save ar.rsc value for "loadrs" */\
	;;							\
.mem.offset 0,0; st8.spill [r16] = r20,16;/* save original r1 */  \
.mem.offset 8,0; st8.spill [r17] = r12,16;			\
	adds r12 = -16,r1;    /* switch to kernel memory stack */  \
	;;							\
.mem.offset 0,0; st8.spill [r16] = r13,16;			\
.mem.offset 8,0; st8.spill [r17] = r10,16;	/* save ar.fpsr */\
	mov r13 = r21;   /* establish `current' */		\
	;;							\
.mem.offset 0,0; st8.spill [r16] = r15,16;			\
.mem.offset 8,0; st8.spill [r17] = r14,16;			\
	;;							\
.mem.offset 0,0; st8.spill [r16] = r2,16;			\
.mem.offset 8,0; st8.spill [r17] = r3,16;			\
	adds r2 = VMM_PT_REGS_R16_OFFSET,r1;			\
	 ;;							\
	adds r16 = VMM_VCPU_IIPA_OFFSET,r13;			\
	adds r17 = VMM_VCPU_ISR_OFFSET,r13;			\
	mov r26 = cr.iipa;					\
	mov r27 = cr.isr;					\
	;;							\
	st8 [r16] = r26;					\
	st8 [r17] = r27;					\
	;;							\
	EXTRA;							\
	mov r8 = ar.ccv;					\
	mov r9 = ar.csd;					\
	mov r10 = ar.ssd;					\
	movl r11 = FPSR_DEFAULT;   /* L-unit */			\
	adds r17 = VMM_VCPU_GP_OFFSET,r13;			\
	;;							\
	ld8 r1 = [r17];/* establish kernel global pointer */	\
	;;							\
	PAL_VSA_SYNC_READ					\
	KVM_MINSTATE_END_SAVE_MIN

/*
 * SAVE_REST saves the remainder of pt_regs (with psr.ic on).
 *
 * Assumed state upon entry:
 *  psr.ic: on
 *  r2: points to &pt_regs.f6
 *  r3: points to &pt_regs.f7
 *  r8: contents of ar.ccv
 *  r9: contents of ar.csd
 *  r10:	contents of ar.ssd
 *  r11:	FPSR_DEFAULT
 *
 * Registers r14 and r15 are guaranteed not to be touched by SAVE_REST.
 */
#define KVM_SAVE_REST				\
.mem.offset 0,0; st8.spill [r2] = r16,16;	\
.mem.offset 8,0; st8.spill [r3] = r17,16;	\
	;;				\
.mem.offset 0,0; st8.spill [r2] = r18,16;	\
.mem.offset 8,0; st8.spill [r3] = r19,16;	\
	;;				\
.mem.offset 0,0; st8.spill [r2] = r20,16;	\
.mem.offset 8,0; st8.spill [r3] = r21,16;	\
	mov r18=b6;			\
	;;				\
.mem.offset 0,0; st8.spill [r2] = r22,16;	\
.mem.offset 8,0; st8.spill [r3] = r23,16;	\
	mov r19 = b7;				\
	;;					\
.mem.offset 0,0; st8.spill [r2] = r24,16;	\
.mem.offset 8,0; st8.spill [r3] = r25,16;	\
	;;					\
.mem.offset 0,0; st8.spill [r2] = r26,16;	\
.mem.offset 8,0; st8.spill [r3] = r27,16;	\
	;;					\
.mem.offset 0,0; st8.spill [r2] = r28,16;	\
.mem.offset 8,0; st8.spill [r3] = r29,16;	\
	;;					\
.mem.offset 0,0; st8.spill [r2] = r30,16;	\
.mem.offset 8,0; st8.spill [r3] = r31,32;	\
	;;					\
	mov ar.fpsr = r11;			\
	st8 [r2] = r8,8;			\
	adds r24 = PT(B6)-PT(F7),r3;		\
	adds r25 = PT(B7)-PT(F7),r3;		\
	;;					\
	st8 [r24] = r18,16;       /* b6 */	\
	st8 [r25] = r19,16;       /* b7 */	\
	adds r2 = PT(R4)-PT(F6),r2;		\
	adds r3 = PT(R5)-PT(F7),r3;		\
	;;					\
	st8 [r24] = r9;	/* ar.csd */		\
	st8 [r25] = r10;	/* ar.ssd */	\
	;;					\
	mov r18 = ar.unat;			\
	adds r19 = PT(EML_UNAT)-PT(R4),r2;	\
	;;					\
	st8 [r19] = r18; /* eml_unat */ 	\


#define KVM_SAVE_EXTRA				\
.mem.offset 0,0; st8.spill [r2] = r4,16;	\
.mem.offset 8,0; st8.spill [r3] = r5,16;	\
	;;					\
.mem.offset 0,0; st8.spill [r2] = r6,16;	\
.mem.offset 8,0; st8.spill [r3] = r7;		\
	;;					\
	mov r26 = ar.unat;			\
	;;					\
	st8 [r2] = r26;/* eml_unat */ 		\

#define KVM_SAVE_MIN_WITH_COVER		KVM_DO_SAVE_MIN(cover, mov r30 = cr.ifs,)
#define KVM_SAVE_MIN_WITH_COVER_R19	KVM_DO_SAVE_MIN(cover, mov r30 = cr.ifs, mov r15 = r19)
#define KVM_SAVE_MIN			KVM_DO_SAVE_MIN(     , mov r30 = r0, )
'#n140'>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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765



















                                                                             
                   


                   
                    
                        


                        
                       
                         
                
 
      
                  









                  






















                             
                     
                                                      
 
                              
 
                                             


                                     










                                                                      


                                                           












                                                                  



















                                                                               
                                          

                              


                         



















                                                                                
                                        




                                                                            
                     
     
                                         
                            

 

                                                               
                                
 
                                  
                                      
                            
               
 


                                
                                                





                                                    
 






                                                                              
                                      








                                                                                          
 

                                                                   
                                                   
 
 



















                                                                                  
                                                            
 
 
 




























































                                                                        


                                                               

                                                    
                              









                                                      



                                                                              



                                            

                                                                                          




                                                    
                     


                                                  




                                                                      
                                                          

 


                                                         










                                                           



                                                                    























                                                                                        
                                                  
                                         
                                 
 









                                                          


                              





























                                                                                       






                                                                  





                                                                   


                                                                










































                                                                   







































































































































                                                                                               


                       
















































































































































                                                                            
/*
 * Copyright (C) 2009, Steven Rostedt <srostedt@redhat.com>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License (not later!)
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <gtk/gtk.h>
#include <glib-object.h>

#include "trace-cmd.h"
#include "trace-local.h"
#include "trace-view.h"
#include "trace-compat.h"
#include "cpu.h"

enum {
	COL_INDEX,
	COL_CPU,
	COL_TS,
	COL_COMM,
	COL_PID,
	COL_LAT,
	COL_EVENT,
	COL_INFO,
	NUM_COLS
};

static char* col_labels[] = {
	"#",
	"CPU",
	"Time Stamp",
	"Task",
	"PID",
	"Latency",
	"Event",
	"Info",
	NULL
};
static int col_chars[] = {
	0,	/* INDEX */
	0,	/* CPU */
	0,	/* TS */
	0,	/* COMM */
	0,	/* PID */
	0,	/* LAT */
	0,	/* EVENT */
	0,	/* INFO */
	0
};

static GtkTreeModel *
create_trace_view_model(struct tracecmd_input *handle)
{
	TraceViewStore *store;

	store = trace_view_store_new(handle);

	return GTK_TREE_MODEL(store);
}

static void
spin_changed(gpointer data, GtkWidget *spin)
{
	GtkTreeView *tree = data;
	GtkTreeModel *model;
	gint val, page;

	val = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spin));

	model = gtk_tree_view_get_model(tree);
	/* This can be called when we NULL out the model */
	if (!model)
		return;
	page = trace_view_store_get_page(TRACE_VIEW_STORE(model));
	if (page == val)
		return;

	g_object_ref(model);
	gtk_tree_view_set_model(tree, NULL);

	trace_view_store_set_page(TRACE_VIEW_STORE(model), val);

	gtk_tree_view_set_model(tree, model);
	g_object_unref(model);
}

void trace_view_data_func(GtkTreeViewColumn *column, GtkCellRenderer *renderer,
			  GtkTreeModel *model, GtkTreeIter *iter,
			  gpointer data)
{
	long col_num = (long)data;
	int str_len, label_len;
	gchar *text, *str;
	int new_w, x_pad;
	GValue val = {0};
	GtkWidget *view;

	PangoFontDescription *pfd;
	PangoLayout *playout;

	/* Put the text in the renderer. */
	gtk_tree_model_get_value(model, iter, col_num, &val);
	g_object_set_property(G_OBJECT(renderer), "text", &val);

	g_object_get(G_OBJECT(renderer),
			"text", &text,
			"font-desc", &pfd,
			NULL);

	if (!text)
		goto out;

	/* Make sure there is enough room to render the column label. */
	str = text;
	str_len = strlen(str);
	label_len = strlen(col_labels[col_num]);
	if (label_len > str_len) {
		str = col_labels[col_num];
		str_len = label_len;
	}

	/* Don't bother with pango unless we have more chars than the max. */
	if (str_len > col_chars[col_num]) {
		col_chars[col_num] = str_len;

		view = GTK_WIDGET(gtk_tree_view_column_get_tree_view(column));
		playout = gtk_widget_create_pango_layout(GTK_WIDGET(view), str);
		pango_layout_set_font_description(playout, pfd);
		pango_layout_get_pixel_size(playout, &new_w, NULL);
		gtk_cell_renderer_get_padding(renderer, &x_pad, NULL);
		/* +10 to avoid another adjustment for one char */
		new_w += 2*x_pad + 10;
		g_object_unref(playout);

		if (new_w > gtk_tree_view_column_get_width(column))
			gtk_tree_view_column_set_fixed_width(column, new_w);
	}

	g_free(text);
 out:
	pango_font_description_free(pfd);
	g_value_unset(&val);
}

void
trace_view_load(GtkWidget *view, struct tracecmd_input *handle,
		GtkWidget *spin)
{
	GtkCellRenderer *renderer;
	GtkCellRenderer *fix_renderer;
	GtkTreeModel *model;
	long c;


	/* --- CPU column --- */

	renderer = gtk_cell_renderer_text_new();
	fix_renderer = gtk_cell_renderer_text_new();

	g_object_set(fix_renderer,
		     "family", "Monospace",
		     "family-set", TRUE,
		     NULL);

	/*
	 * Set fixed height mode now which will cause all the columns below to
	 * be created with their sizing property to be set to
	 * GTK_TREE_VIEW_COLUMN_FIXED.
	 */
	gtk_tree_view_set_fixed_height_mode(GTK_TREE_VIEW(view), TRUE);

	for (c = 0; c < NUM_COLS; c++)
	{
		gtk_tree_view_insert_column_with_data_func(GTK_TREE_VIEW(view),
				-1,
				col_labels[c],
				(c == COL_LAT || c == COL_INFO) ? fix_renderer : renderer,
				trace_view_data_func,
				(gpointer)c,
				NULL);
	}

	g_signal_connect_swapped (G_OBJECT (spin), "value-changed",
				  G_CALLBACK (spin_changed),
				  (gpointer) view);


	if (handle) {
		model = create_trace_view_model(handle);
		trace_view_store_set_spin_button(TRACE_VIEW_STORE(model), spin);
		gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
		g_object_unref(model); /* destroy model automatically with view */
	}
}

void trace_view_reload(GtkWidget *view, struct tracecmd_input *handle,
		       GtkWidget *spin)
{
	GtkTreeModel *model;

	if (!handle)
		return;

	model = create_trace_view_model(handle);
	if (!model)
		return;
	trace_view_store_set_spin_button(TRACE_VIEW_STORE(model), spin);
	gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);

}

/**
 * trace_view_get_selected_row - return the selected row
 * @treeview: The tree view
 *
 * Returns the selected row number (or -1 if none is selected)
 */
gint trace_view_get_selected_row(GtkWidget *treeview)
{
	GtkTreeView *tree = GTK_TREE_VIEW(treeview);
	GtkTreeSelection *selection;
	GtkTreeModel *model;
	GtkTreePath *path;
	gchar *spath;
	GList *glist;
	gint row;

	model = gtk_tree_view_get_model(tree);
	if (!model)
		return -1;

	selection = gtk_tree_view_get_selection(tree);
	glist = gtk_tree_selection_get_selected_rows(selection, &model);
	if (!glist)
		return -1;

	/* Only one row may be selected */
	path = glist->data;
	spath = gtk_tree_path_to_string(path);
	row = atoi(spath);
	g_free(spath);

	gtk_tree_path_free(path);
	g_list_free(glist);

	return row;
}

void trace_view_make_selection_visible(GtkWidget *treeview)
{
	GtkTreeView *tree = GTK_TREE_VIEW(treeview);
	GtkTreePath *path;
	gchar *spath;
	GString *gstr;
	gint row;

	row = trace_view_get_selected_row(treeview);
	if (row < 0)
		return;

	gstr = g_string_new("");
	g_string_printf(gstr, "%d", row);
	spath = g_string_free(gstr, FALSE);

	path = gtk_tree_path_new_from_string(spath);
	g_free(spath);

	gtk_tree_view_scroll_to_cell(tree, path, NULL, TRUE, 0.5, 0.0);

	gtk_tree_path_free(path);
}

void trace_view_update_filters(GtkWidget *treeview,
			       struct filter_task *task_filter,
			       struct filter_task *hide_tasks)
{
	GtkTreeView *tree = GTK_TREE_VIEW(treeview);
	TraceViewRecord *vrec;
	GtkTreeModel *model;
	guint64 time;
	gint row;

	model = gtk_tree_view_get_model(tree);
	if (!model)
		return;

	/* Keep track of the currently selected row */
	row = trace_view_get_selected_row(treeview);
	if (row >= 0) {
		vrec = trace_view_store_get_row(TRACE_VIEW_STORE(model), row);
		time = vrec->timestamp;
	}

	g_object_ref(model);
	gtk_tree_view_set_model(tree, NULL);

	trace_view_store_assign_filters(TRACE_VIEW_STORE(model), task_filter, hide_tasks);
	trace_view_store_update_filter(TRACE_VIEW_STORE(model));

	gtk_tree_view_set_model(tree, model);
	g_object_unref(model);

	/* Keep selection near previous selection */
	if (row >= 0)
		trace_view_select(treeview, time);
}

static void select_row_from_path(GtkTreeView *tree, GtkTreePath *path)
{
	GtkTreeSelection *selection;

	selection = gtk_tree_view_get_selection(tree);
	gtk_tree_view_set_cursor(tree, path, NULL, FALSE);
}

void trace_view_select(GtkWidget *treeview, guint64 time)
{
	GtkTreeView *tree = GTK_TREE_VIEW(treeview);
	GtkTreeModel *model;
	GtkTreePath *path;
	gint select_page, page;
	GtkWidget *spin;
	gchar buf[100];
	gint row;

	model = gtk_tree_view_get_model(tree);
	/* This can be called when we NULL out the model */
	if (!model)
		return;

	if (!trace_view_store_visible_rows(TRACE_VIEW_STORE(model)))
		return;

	page = trace_view_store_get_page(TRACE_VIEW_STORE(model));
	select_page = trace_view_store_get_timestamp_page(TRACE_VIEW_STORE(model),
							  time);

	/* Make sure the page contains the selected event */
	if (page != select_page) {
		spin = trace_view_store_get_spin(TRACE_VIEW_STORE(model));
		/* If a spin button exists, it should update when changed */
		if (spin)
			gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), select_page);
		else {
			g_object_ref(model);
			gtk_tree_view_set_model(tree, NULL);

			trace_view_store_set_page(TRACE_VIEW_STORE(model), select_page);

			gtk_tree_view_set_model(tree, model);
			g_object_unref(model);
		}
	}

	/* Select the event */
	row = trace_view_store_get_timestamp_visible_row(TRACE_VIEW_STORE(model), time);
	snprintf(buf, 100, "%d", row);
	path = gtk_tree_path_new_from_string(buf);
	select_row_from_path(tree, path);
	gtk_tree_path_free(path);
}

void trace_view_event_filter_callback(gboolean accept,
				      gboolean all_events,
				      gchar **systems,
				      gint *events,
				      gpointer data)
{
	GtkTreeView *trace_tree = data;
	GtkTreeModel *model;
	TraceViewStore *store;
	TraceViewRecord *vrec;
	guint64 time;
	gint row;
	gint i;

	if (!accept)
		return;

	model = gtk_tree_view_get_model(trace_tree);
	if (!model)
		return;

	store = TRACE_VIEW_STORE(model);

	if (all_events) {
		if (trace_view_store_get_all_events_enabled(store))
			return;

		trace_view_store_set_all_events_enabled(store);
	} else {
		trace_view_store_clear_all_events_enabled(store);

		if (systems) {
			for (i = 0; systems[i]; i++)
				trace_view_store_set_system_enabled(store, systems[i]);
		}

		if (events) {
			for (i = 0; events[i] >= 0; i++)
				trace_view_store_set_event_enabled(store, events[i]);
		}
	}

	/* Keep track of the currently selected row */
	row = trace_view_get_selected_row(GTK_WIDGET(trace_tree));
	if (row >= 0) {
		vrec = trace_view_store_get_row(store, row);
		time = vrec->timestamp;
	}

	/* Force an update */
	g_object_ref(store);
	gtk_tree_view_set_model(trace_tree, NULL);
	trace_view_store_update_filter(store);
	gtk_tree_view_set_model(trace_tree, GTK_TREE_MODEL(store));
	g_object_unref(store);

	if (row >= 0)
		trace_view_select(GTK_WIDGET(trace_tree), time);
}

void trace_view_cpu_filter_callback(gboolean accept,
				    gboolean all_cpus,
				    guint64 *selected_cpu_mask,
				    gpointer data)
{
	GtkTreeView *trace_tree = data;
	GtkTreeModel *model;
	TraceViewStore *store;
	gint cpus;
	gint cpu;

	model = gtk_tree_view_get_model(trace_tree);
	if (!model)
		return;

	store = TRACE_VIEW_STORE(model);

	if (!accept)
		return;

	g_object_ref(store);
	gtk_tree_view_set_model(trace_tree, NULL);

	if (all_cpus) {
		trace_view_store_set_all_cpus(store);
		goto set_model;
	}

	cpus = trace_view_store_get_cpus(store);

	for (cpu = 0; cpu < cpus; cpu++) {
		if (cpu_isset(selected_cpu_mask, cpu))
			trace_view_store_set_cpu(store, cpu);
		else
			trace_view_store_clear_cpu(store, cpu);
	}

 set_model:
	gtk_tree_view_set_model(trace_tree, GTK_TREE_MODEL(store));
	g_object_unref(store);
}

static GtkTreeModel *create_col_model(GtkTreeView *treeview)
{
	GtkListStore *store;
	GtkTreeViewColumn *col;
	GtkTreeIter iter;
	const gchar *title;
	int i;

	store = gtk_list_store_new(1, G_TYPE_STRING);

	i = 0;
	col = gtk_tree_view_get_column(treeview, i++);
	while (col) {
		title = gtk_tree_view_column_get_title(col);
		if (!title)
			break;

		gtk_list_store_append(store, &iter);
		gtk_list_store_set(store, &iter,
				   0, title,
				   -1);

		col = gtk_tree_view_get_column(treeview, i++);
	}

	return GTK_TREE_MODEL(store);
}

struct search_info {
	GtkTreeView		*treeview;
	GtkWidget		*entry;
	GtkWidget		*selection;
	GtkWidget		*column;
};

#define SELECTION_NAMES					\
	C(	contains,	CONTAINS	),	\
	C(	full match,	FULL_MATCH	),	\
	C(	does not have,	NOT_IN		)

#undef C
#define C(a, b)	#a

static gchar *select_names[] = { SELECTION_NAMES, NULL };

#undef C
#define C(a, b) SEL_##b

enum select_options { SELECTION_NAMES };

static gboolean test_int(gint val, gint search_val, enum select_options sel)
{
	gint tens;
	gboolean match = TRUE;

	switch (sel) {
	case SEL_NOT_IN:
		match = FALSE;
	case SEL_CONTAINS:
		for (tens = 1; search_val / tens; tens *= 10)
			;

		while (val) {
			if (val - search_val == (val / tens) * tens)
				return match;
			val /= 10;
		}
		return !match;

	case SEL_FULL_MATCH:
		return search_val == val;
	}
	return FALSE;
}

static gboolean test_text(const gchar *text, const gchar *search_text, enum select_options sel)
{
	gboolean match = TRUE;

	switch (sel) {
	case SEL_NOT_IN:
		match = FALSE;
	case SEL_CONTAINS:

		text = strcasestr(text, search_text);
		if (text)
			return match;
		return !match;

	case SEL_FULL_MATCH:
		return strcmp(text, search_text) == 0;
	}
	return FALSE;
}

static void search_tree(gpointer data)
{
	struct search_info *info = data;
	GtkTreePath *path;
	GtkTreeViewColumn *col;
	GtkTreeModel *model;
	TraceViewStore *store;
	GtkTreeIter iter;
	GtkEntry *entry = GTK_ENTRY(info->entry);
	GtkComboBox *col_combo = GTK_COMBO_BOX(info->column);
	GtkComboBox *sel_combo = GTK_COMBO_BOX(info->selection);
	const gchar *title;
	gint val;
	gchar *text = NULL;
	const gchar *search_text;
	gint col_num;
	gint sel;
	gint search_val;
	gint start_row;
	gboolean found = FALSE;

	col_num = gtk_combo_box_get_active(col_combo);
	sel = gtk_combo_box_get_active(sel_combo);

	if (col_num >= TRACE_VIEW_STORE_N_COLUMNS)
		return;

	search_text = gtk_entry_get_text(entry);
	if (!search_text || !strlen(search_text))
		return;

	col = gtk_tree_view_get_column(info->treeview, col_num);
	if (!col)
		return;

	title = gtk_tree_view_column_get_title(col);
	if (!title)
		return;

	model = gtk_tree_view_get_model(info->treeview);
	if (!model)
		return;

	store = TRACE_VIEW_STORE(model);

	if (!trace_view_store_visible_rows(store))
		return;

	start_row = trace_view_get_selected_row(GTK_WIDGET(info->treeview));
	if (start_row < 0)
		start_row = 0;

	if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, start_row))
		return;

	search_val = atoi(search_text);
	while (gtk_tree_model_iter_next(model, &iter)) {
		switch (col_num) {
		case TRACE_VIEW_STORE_COL_INDEX:
		case TRACE_VIEW_STORE_COL_CPU:
		case TRACE_VIEW_STORE_COL_PID:
		/* integers */

			gtk_tree_model_get(model, &iter,
					   col_num, &val,
					   -1);
			if (test_int(val, search_val, sel))
				found = TRUE;
			break;

		case TRACE_VIEW_STORE_COL_TS:
		case TRACE_VIEW_STORE_COL_COMM:
		case TRACE_VIEW_STORE_COL_LAT:
		case TRACE_VIEW_STORE_COL_EVENT:
		case TRACE_VIEW_STORE_COL_INFO:
			/* strings */

			gtk_tree_model_get(model, &iter,
					   col_num, &text,
					   -1);

			if (test_text(text, search_text, sel))
				found = TRUE;
			break;
		}

		if (found)
			break;
	}


	if (!found) {
		printf("NOT FOUND!\n");
		/* show pop up */
		return;
	}

	path = gtk_tree_model_get_path(model, &iter);
	select_row_from_path(info->treeview, path);
	gtk_tree_path_free(path);
}

void trace_view_search_setup(GtkBox *box, GtkTreeView *treeview)
{
	GtkCellRenderer *renderer;
	GtkListStore *store;
	GtkTreeModel *model;
	GtkTreeIter iter;
	GtkWidget *label;
	GtkWidget *col_combo;
	GtkWidget *sel_combo;
	GtkWidget *entry;
	gchar **selects = select_names;
	int i;
	struct search_info *info;

	renderer = gtk_cell_renderer_text_new();

	info = g_new0(typeof(*info), 1);
	info->treeview = treeview;

	label = gtk_label_new("Column: ");
	gtk_box_pack_start(box, label, FALSE, FALSE, 0);
	gtk_widget_show(label);

	/* --- Set up the column selection combo box --- */

	model = create_col_model(treeview);

	col_combo = gtk_combo_box_new_with_model(model);
	gtk_box_pack_start(box, col_combo, FALSE, FALSE, 0);
	gtk_widget_show(col_combo);

	/* Free model with combobox */
	g_object_unref(model);

	gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(col_combo),
				   renderer,
				   TRUE);
	gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(col_combo),
				       renderer,
				       "text", 0,
				       NULL);

	gtk_combo_box_set_active(GTK_COMBO_BOX(col_combo), 0);

	info->column = col_combo;

	/* --- Set up the column selection combo box --- */

	store = gtk_list_store_new(1, G_TYPE_STRING);
	model = GTK_TREE_MODEL(store);

	sel_combo = gtk_combo_box_new_with_model(model);
	gtk_box_pack_start(box, sel_combo, FALSE, FALSE, 0);
	gtk_widget_show(sel_combo);

	info->selection = sel_combo;

	gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(sel_combo),
				   renderer,
				   TRUE);
	gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(sel_combo),
				       renderer,
				       "text", 0,
				       NULL);

	for (i = 0; selects[i]; i++ ) {
		gtk_list_store_append(store, &iter);
		gtk_list_store_set(store, &iter,
				   0, selects[i],
				   -1);
	}

	gtk_combo_box_set_active(GTK_COMBO_BOX(sel_combo), 0);

	/* --- The text entry --- */

	entry = gtk_entry_new();
	gtk_box_pack_start(box, entry, FALSE, FALSE, 0);
	gtk_widget_show(entry);

	info->entry = entry;

	g_signal_connect_swapped (entry, "activate",
				  G_CALLBACK (search_tree),
				  (gpointer) info);
}